jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-12 00:20 UTC
Pass multiple form element values with link_to_remote?
Let me preface this by noting that I''m new to Rails and hardly know anything about Javascript...hence why I need help. :) That said, I have a form which contains three text fields: site_id, start_date, and end_date. I have a link_to_remote link which needs to pass these three values to an action when I click it, but I can''t for the life of me figure out how to access / pass the three text field values on so that I can use them. Any help for me? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wildtangent
2008-Jan-12 12:31 UTC
Re: Pass multiple form element values with link_to_remote?
you probably should make it a link_to_function instead which calls
form.submit() (where form is the id of your form). Then turn your form
into a remote_form_tag or remote_form_for. I think this would work
because you are triggering the onsubmit handler of the form which will
contain the AJAX request.
e.g. (approximately)
<%=
link_to_function(''save'',''my_form.submit();'')
%>
<%= image_tag(''/images/spinner.gif'', :alt =>
''Loading''), :id =>
''spinner'' %>
<%= form_remote_tag(:url => {:controller => ''sites'',
:action =>
''create''}, :before =>
"Element.show(''spinner'')", :after =>
"Element.hide(''spinner'')", :html => {:id =>
''my_form''}) do %>
<%= text_field_tag(:site_id) %>
<%= text_field_tag(:start_date) %>
<%= text_field_tag(:end_date) %>
<% end %>
Not too familiar with the exact syntax for form_remote_tag, but it is
along those lines. Try gotapi.com/rubyrails for the full reference.
I''ve added the image there for a loading spinner but you could equally
write some text or whatever, just make sure there is an element with
id=''spinner'', or change the :before and :after calls (or
remove them).
You can also use remote_form_for if you are changing a model directly.
I prefer this way. Finally, you can use restful or named routes in
place of the {:controller => ... :action => ...} hash
If you want to know more about selecting elements from the DOM using
prototype''s $ helpers check that out on the prototype webiste
http://www.prototypejs.org/
Hope this assists.
Joe
On Jan 12, 12:20 am,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Let me preface this by noting that I''m new to Rails and hardly
know
> anything about Javascript...hence why I need help. :)
>
> That said, I have a form which contains three text fields: site_id,
> start_date, and end_date. I have a link_to_remote link which needs to
> pass these three values to an action when I click it, but I can''t
for
> the life of me figure out how to access / pass the three text field
> values on so that I can use them.
>
> Any help for me?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I actually got what I wanted using the link_to_remote. I''m not sure
the form_remote_tag was exactly what I wanted, but I appreciate the
help because I will certainly need that later on with what I''m doing.
The solution was this:
In my view:
<p><%= link_to_remote("Calculate Invoice",
:url => { :action => :calculate_invoice },
:method => ''get'',
:with => ''serialize_fields()'',
:update => ''invoice-div'' )%>
<h2>Invoice Amount</h2>
<p>
<div id="invoice-div" name="invoice-div">Pending
selection...</div>
In application.js: (code stolen from another post in the group)
function serialize_fields(){
var fields = new Array();
fields.push(Form.Element.serialize(''reservation_site_id''));
fields.push(Form.Element.serialize(''reservation_start_date''));
fields.push(Form.Element.serialize(''reservation_end_date''));
return fields.join(''&'');
}
In my controller (this is ugly as sin and there are probably a ton of
better ways to do this:
def calculate_invoice
@site = Site.find(params[:reservation][:site_id].to_s)
nightly_rate = @site.default_rate
start_date = params[:reservation][:start_date].to_date
end_date = params[:reservation][:end_date].to_date
total_nights = end_date - start_date
invoice_total = nightly_rate * total_nights
render :partial => ''invoice'', :locals => {
:nightly_rate =>
nightly_rate.to_s, :total_nights => total_nights.to_s, :invoice_total
=> invoice_total.to_s }
end
And then my _invoice.rhtml:
<table>
<tr>
<th>Total Nights</th>
<th>Nightly Rate</th>
<th>Invoice Total</th>
</tr>
<tr>
<td><%= total_nights %></td>
<td><%= nightly_rate %></td>
<td><%= invoice_total %></td>
</tr>
</table>
<input type="hidden" value="<%= invoice_total %>"
id="reservation_invoice_amount"
name="reservation[invoice_amount]">
Anybody feel free to advise me where I could have done things better
because I''m still very very new to coding in general, and especially
new to Rails.
Thanks!
On Jan 12, 7:31 am, Wildtangent
<wildtang...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> you probably should make it a link_to_function instead which calls
> form.submit() (where form is the id of your form). Then turn your form
> into a remote_form_tag or remote_form_for. I think this would work
> because you are triggering the onsubmit handler of the form which will
> contain the AJAX request.
>
> e.g. (approximately)
>
> <%=
link_to_function(''save'',''my_form.submit();'')
%>
> <%= image_tag(''/images/spinner.gif'', :alt =>
''Loading''), :id =>
> ''spinner'' %>
> <%= form_remote_tag(:url => {:controller =>
''sites'', :action =>
> ''create''}, :before =>
"Element.show(''spinner'')", :after =>
> "Element.hide(''spinner'')", :html => {:id
=> ''my_form''}) do %>
> <%= text_field_tag(:site_id) %>
> <%= text_field_tag(:start_date) %>
> <%= text_field_tag(:end_date) %>
> <% end %>
>
> Not too familiar with the exact syntax for form_remote_tag, but it is
> along those lines. Try gotapi.com/rubyrails for the full reference.
> I''ve added the image there for a loading spinner but you could
equally
> write some text or whatever, just make sure there is an element with
> id=''spinner'', or change the :before and :after calls (or
remove them).
>
> You can also use remote_form_for if you are changing a model directly.
> I prefer this way. Finally, you can use restful or named routes in
> place of the {:controller => ... :action => ...} hash
>
> If you want to know more about selecting elements from the DOM using
> prototype''s $ helpers check that out on the prototype
webistehttp://www.prototypejs.org/
>
> Hope this assists.
>
> Joe
>
> On Jan 12, 12:20 am,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
> > Let me preface this by noting that I''m new to Rails and
hardly know
> > anything about Javascript...hence why I need help. :)
>
> > That said, I have a form which contains three text fields: site_id,
> > start_date, and end_date. I have a link_to_remote link which needs to
> > pass these three values to an action when I click it, but I
can''t for
> > the life of me figure out how to access / pass the three text field
> > values on so that I can use them.
>
> > Any help for me?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
stephen.celis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-12 19:46 UTC
Re: Pass multiple form element values with link_to_remote?
You can pass any params you wish in the :url hash
In this case:
<%= link_to_remote("Calculate Invoice",
:url => { :action => :calculate_invoice, :start_date =>
params[:reservation][:start_date], :end_date => params[:reservation]
[:end_date] },
:method => ''get'',
:update => ''invoice-div'' )%>
Then find total_nights in the calculate_invoice controller action.
On Jan 12, 12:45 pm, Jeremy
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I actually got what I wanted using the link_to_remote. I''m not
sure
> the form_remote_tag was exactly what I wanted, but I appreciate the
> help because I will certainly need that later on with what I''m
doing.
> The solution was this:
>
> In my view:
>
> <p><%= link_to_remote("Calculate Invoice",
> :url => { :action => :calculate_invoice },
> :method => ''get'',
> :with => ''serialize_fields()'',
> :update => ''invoice-div'' )%>
>
> <h2>Invoice Amount</h2>
> <p>
> <div id="invoice-div"
name="invoice-div">Pending selection...</div>
>
> In application.js: (code stolen from another post in the group)
>
> function serialize_fields(){
> var fields = new Array();
>
fields.push(Form.Element.serialize(''reservation_site_id''));
>
fields.push(Form.Element.serialize(''reservation_start_date''));
>
fields.push(Form.Element.serialize(''reservation_end_date''));
> return fields.join(''&'');
>
> }
>
> In my controller (this is ugly as sin and there are probably a ton of
> better ways to do this:
>
> def calculate_invoice
> @site = Site.find(params[:reservation][:site_id].to_s)
> nightly_rate = @site.default_rate
> start_date = params[:reservation][:start_date].to_date
> end_date = params[:reservation][:end_date].to_date
> total_nights = end_date - start_date
> invoice_total = nightly_rate * total_nights
> render :partial => ''invoice'', :locals
=> { :nightly_rate =>
> nightly_rate.to_s, :total_nights => total_nights.to_s, :invoice_total
> => invoice_total.to_s }
> end
>
> And then my _invoice.rhtml:
>
> <table>
> <tr>
> <th>Total Nights</th>
> <th>Nightly Rate</th>
> <th>Invoice Total</th>
> </tr>
> <tr>
> <td><%= total_nights %></td>
> <td><%= nightly_rate %></td>
> <td><%= invoice_total %></td>
> </tr>
> </table>
>
> <input type="hidden" value="<%= invoice_total
%>"
> id="reservation_invoice_amount"
name="reservation[invoice_amount]">
>
> Anybody feel free to advise me where I could have done things better
> because I''m still very very new to coding in general, and
especially
> new to Rails.
>
> Thanks!
>
> On Jan 12, 7:31 am, Wildtangent
<wildtang...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > you probably should make it a link_to_function instead which calls
> > form.submit() (where form is the id of your form). Then turn your form
> > into a remote_form_tag or remote_form_for. I think this would work
> > because you are triggering the onsubmit handler of the form which will
> > contain the AJAX request.
>
> > e.g. (approximately)
>
> > <%=
link_to_function(''save'',''my_form.submit();'')
%>
> > <%= image_tag(''/images/spinner.gif'', :alt =>
''Loading''), :id =>
> > ''spinner'' %>
> > <%= form_remote_tag(:url => {:controller =>
''sites'', :action =>
> > ''create''}, :before =>
"Element.show(''spinner'')", :after =>
> > "Element.hide(''spinner'')", :html =>
{:id => ''my_form''}) do %>
> > <%= text_field_tag(:site_id) %>
> > <%= text_field_tag(:start_date) %>
> > <%= text_field_tag(:end_date) %>
> > <% end %>
>
> > Not too familiar with the exact syntax for form_remote_tag, but it is
> > along those lines. Try gotapi.com/rubyrails for the full reference.
> > I''ve added the image there for a loading spinner but you
could equally
> > write some text or whatever, just make sure there is an element with
> > id=''spinner'', or change the :before and :after calls
(or remove them).
>
> > You can also use remote_form_for if you are changing a model directly.
> > I prefer this way. Finally, you can use restful or named routes in
> > place of the {:controller => ... :action => ...} hash
>
> > If you want to know more about selecting elements from the DOM using
> > prototype''s $ helpers check that out on the prototype
webistehttp://www.prototypejs.org/
>
> > Hope this assists.
>
> > Joe
>
> > On Jan 12, 12:20 am,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
>
> > > Let me preface this by noting that I''m new to Rails and
hardly know
> > > anything about Javascript...hence why I need help. :)
>
> > > That said, I have a form which contains three text fields:
site_id,
> > > start_date, and end_date. I have a link_to_remote link which
needs to
> > > pass these three values to an action when I click it, but I
can''t for
> > > the life of me figure out how to access / pass the three text
field
> > > values on so that I can use them.
>
> > > Any help for me?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
stephen.celis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-12 19:52 UTC
Re: Pass multiple form element values with link_to_remote?
Oops, I mixed up information. Really, what you wanted to use was a remote_form, though, as wildtangent suggested. Use a form for multiple values that are entered on the page after the page loads. Use a link/button for any action using params that were already available from the controller action. On Jan 12, 1:46 pm, "stephen.ce...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <stephen.ce...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can pass any params you wish in the :url hash > > In this case: > <%= link_to_remote("Calculate Invoice", > :url => { :action => :calculate_invoice, :start_date => > params[:reservation][:start_date], :end_date => params[:reservation] > [:end_date] }, > :method => ''get'', > :update => ''invoice-div'' )%> > > Then find total_nights in the calculate_invoice controller action. > > On Jan 12, 12:45 pm, Jeremy <jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I actually got what I wanted using the link_to_remote. I''m not sure > > the form_remote_tag was exactly what I wanted, but I appreciate the > > help because I will certainly need that later on with what I''m doing. > > The solution was this: > > > In my view: > > > <p><%= link_to_remote("Calculate Invoice", > > :url => { :action => :calculate_invoice }, > > :method => ''get'', > > :with => ''serialize_fields()'', > > :update => ''invoice-div'' )%> > > > <h2>Invoice Amount</h2> > > <p> > > <div id="invoice-div" name="invoice-div">Pending selection...</div> > > > In application.js: (code stolen from another post in the group) > > > function serialize_fields(){ > > var fields = new Array(); > > fields.push(Form.Element.serialize(''reservation_site_id'')); > > fields.push(Form.Element.serialize(''reservation_start_date'')); > > fields.push(Form.Element.serialize(''reservation_end_date'')); > > return fields.join(''&''); > > > } > > > In my controller (this is ugly as sin and there are probably a ton of > > better ways to do this: > > > def calculate_invoice > > @site = Site.find(params[:reservation][:site_id].to_s) > > nightly_rate = @site.default_rate > > start_date = params[:reservation][:start_date].to_date > > end_date = params[:reservation][:end_date].to_date > > total_nights = end_date - start_date > > invoice_total = nightly_rate * total_nights > > render :partial => ''invoice'', :locals => { :nightly_rate => > > nightly_rate.to_s, :total_nights => total_nights.to_s, :invoice_total > > => invoice_total.to_s } > > end > > > And then my _invoice.rhtml: > > > <table> > > <tr> > > <th>Total Nights</th> > > <th>Nightly Rate</th> > > <th>Invoice Total</th> > > </tr> > > <tr> > > <td><%= total_nights %></td> > > <td><%= nightly_rate %></td> > > <td><%= invoice_total %></td> > > </tr> > > </table> > > > <input type="hidden" value="<%= invoice_total %>" > > id="reservation_invoice_amount" name="reservation[invoice_amount]"> > > > Anybody feel free to advise me where I could have done things better > > because I''m still very very new to coding in general, and especially > > new to Rails. > > > Thanks! > > > On Jan 12, 7:31 am, Wildtangent <wildtang...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > you probably should make it a link_to_function instead which calls > > > form.submit() (where form is the id of your form). Then turn your form > > > into a remote_form_tag or remote_form_for. I think this would work > > > because you are triggering the onsubmit handler of the form which will > > > contain the AJAX request. > > > > e.g. (approximately) > > > > <%= link_to_function(''save'',''my_form.submit();'') %> > > > <%= image_tag(''/images/spinner.gif'', :alt => ''Loading''), :id => > > > ''spinner'' %> > > > <%= form_remote_tag(:url => {:controller => ''sites'', :action => > > > ''create''}, :before => "Element.show(''spinner'')", :after => > > > "Element.hide(''spinner'')", :html => {:id => ''my_form''}) do %> > > > <%= text_field_tag(:site_id) %> > > > <%= text_field_tag(:start_date) %> > > > <%= text_field_tag(:end_date) %> > > > <% end %> > > > > Not too familiar with the exact syntax for form_remote_tag, but it is > > > along those lines. Try gotapi.com/rubyrails for the full reference. > > > I''ve added the image there for a loading spinner but you could equally > > > write some text or whatever, just make sure there is an element with > > > id=''spinner'', or change the :before and :after calls (or remove them). > > > > You can also use remote_form_for if you are changing a model directly. > > > I prefer this way. Finally, you can use restful or named routes in > > > place of the {:controller => ... :action => ...} hash > > > > If you want to know more about selecting elements from the DOM using > > > prototype''s $ helpers check that out on the prototype webistehttp://www.prototypejs.org/ > > > > Hope this assists. > > > > Joe > > > > On Jan 12, 12:20 am, "jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > Let me preface this by noting that I''m new to Rails and hardly know > > > > anything about Javascript...hence why I need help. :) > > > > > That said, I have a form which contains three text fields: site_id, > > > > start_date, and end_date. I have a link_to_remote link which needs to > > > > pass these three values to an action when I click it, but I can''t for > > > > the life of me figure out how to access / pass the three text field > > > > values on so that I can use them. > > > > > Any help for me?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just reading this thread made me wonder. I thought serialize was a standard function. Having looked, and, yes serialize is in the Scriptaculous library, so you don''t need to write the function. Of course you may have written your own because you only want to include a subset of the fields. Also noticed this thread which included a subset function - which you may already have seen. http://groups.google.ca/group/rubyonrails-talk/msg/3df638d6c271d8c0 Tonypm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---