Hi, rails 3.2.8 I must be missing something basic (and it''s been driving me mad) because I am trying to send one additional data attribute to my controller from a link, but I can''t seem to get it into the params received by the controller. I have looked around and found answers, but none seem to affect my links. I''ve set up a dummy app with a home page and a single link to a contacts page - here is the code: routes.rb root :to => ''home#index'', :as => ''home'' resource ''contacts'' Haml Home Page View # I want to be able to display ''bar'' on the contacts page = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} The generated html link: <a href="/contacts <http://localhost:3000/contacts>" data-foo="bar">contacts </a> But I don''t receive the foo attribute in the params object in the show action of ContactsController. If I add {:method => :post} to link_to, then it routes to the create action of the ContactsController but again, foo is missing. = link_to "Contacts", contacts_path, {:method => :post, :data => {:foo => ''bar''}} generates: <a href="/contacts <http://localhost:3000/contacts>" data-foo="bar" data-method="post" rel="nofollow">contacts</a> Can anyone point me in the right direction? Thanks Paul -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/YrXt6ireP7oJ. For more options, visit https://groups.google.com/groups/opt_out.
Am 10.10.2012 09:47 schrieb "paul h" <paul-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org>:> # I want to be able to display ''bar'' on the contacts page > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''}Should be params=>.... I here if I recall correctly. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi Norbert, Thanks for the reply, but can you elaborate a little? On Wednesday, October 10, 2012 12:01:54 PM UTC+1, Norbert Melzer wrote:> > > Am 10.10.2012 09:47 schrieb "paul h" <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org <javascript:>>: > > > # I want to be able to display ''bar'' on the contacts page > > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} > > Should be params=>.... I here if I recall correctly. >Doesn''t appear to be, replacing :data with :params changes the HTML link as follows: data-foo=''bar'' becomes params="{:foo=>"bar"}" Changing :data to params just mashes up the html link. AFAIK the generated HTML link is correct data-foo=''bar'', shouldn''t this be accessible in the controller as part of the params hash? If I inspect the params hash, all that is there is the usual key/value pairs: controller, action, (when applicable => id, method, authenticity_token). How can I send an additional data attribute to the controller? Do I have problem with my routes? Thanks Paul -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/lN53bwOAiNoJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wed, Oct 10, 2012 at 11:25 PM, paul h <paul-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org> wrote:> Hi Norbert, > > Thanks for the reply, but can you elaborate a little? > > On Wednesday, October 10, 2012 12:01:54 PM UTC+1, Norbert Melzer wrote: >> >> >> Am 10.10.2012 09:47 schrieb "paul h" <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org>: >> >> > # I want to be able to display ''bar'' on the contacts page >> > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} >> >> Should be params=>.... I here if I recall correctly. >> > > Doesn''t appear to be, replacing :data with :params changes the HTML link > as follows: > data-foo=''bar'' becomes params="{:foo=>"bar"}" > > Changing :data to params just mashes up the html link. > > AFAIK the generated HTML link is correct data-foo=''bar'', shouldn''t this be > accessible in the controller as part of the params hash? > > If I inspect the params hash, all that is there is the usual key/value > pairs: controller, action, (when applicable => id, method, > authenticity_token). > > How can I send an additional data attribute to the controller? Do I have > problem with my routes? >pass it on the named route link_to ''foo'', foo_path(:foo => ''bar'', :blah => ''baz'')> > Thanks > > Paul > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/lN53bwOAiNoJ. > > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 10 October 2012 08:47, paul h <paul-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org> wrote:> Hi, > > rails 3.2.8 > > I must be missing something basic (and it''s been driving me mad) because I > am trying to send one additional data attribute to my controller from a > link, but I can''t seem to get it into the params received by the controller. > > I have looked around and found answers, but none seem to affect my links. > > I''ve set up a dummy app with a home page and a single link to a contacts > page - here is the code: > > routes.rb > > root :to => ''home#index'', :as => ''home'' > resource ''contacts'' > > Haml Home Page View > > # I want to be able to display ''bar'' on the contacts page > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} > > The generated html link: > > <a href="/contacts" data-foo="bar">contacts</a> > > But I don''t receive the foo attribute in the params object in the show > action of ContactsController. > > If I add {:method => :post} to link_to, then it routes to the create action > of the ContactsController but again, foo is missing. > > = link_to "Contacts", contacts_path, {:method => :post, :data => {:foo => > ''bar''}} > > generates: > > <a href="/contacts" data-foo="bar" data-method="post" > rel="nofollow">contacts</a>What do you see in development.log when you click the link. It should show you the parameters being passed. Also if you do params.inspect what do you see? You can put that in a view or use a debugger. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, 10 October 2012 06:25:25 UTC-7, paul h wrote:> > Hi Norbert, > > Thanks for the reply, but can you elaborate a little? > > On Wednesday, October 10, 2012 12:01:54 PM UTC+1, Norbert Melzer wrote: >> >> >> Am 10.10.2012 09:47 schrieb "paul h" <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org>: >> >> > # I want to be able to display ''bar'' on the contacts page >> > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} >> >> Should be params=>.... I here if I recall correctly. >> > > Doesn''t appear to be, replacing :data with :params changes the HTML link > as follows: > data-foo=''bar'' becomes params="{:foo=>"bar"}" > > Changing :data to params just mashes up the html link. > > AFAIK the generated HTML link is correct data-foo=''bar'', shouldn''t this be > accessible in the controller as part of the params hash? > > If I inspect the params hash, all that is there is the usual key/value > pairs: controller, action, (when applicable => id, method, > authenticity_token). > > How can I send an additional data attribute to the controller? Do I have > problem with my routes? > >Data-attributes are intended for code *on* the page (JS, etc) to use - they don''t get sent to the server unless you''re doing something fancy with JS to send them. You''ll want to pass additional parameters to the URL helper (contacts_path in your example) to get them in the controller. --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8lG-nXoI2vYJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, October 10, 2012 2:30:57 PM UTC+1, jim wrote:> > > > On Wed, Oct 10, 2012 at 11:25 PM, paul h <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org<javascript:> > > wrote: > >> Hi Norbert, >> >> Thanks for the reply, but can you elaborate a little? >> >> On Wednesday, October 10, 2012 12:01:54 PM UTC+1, Norbert Melzer wrote: >>> >>> >>> Am 10.10.2012 09:47 schrieb "paul h" <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org>: >>> >>> > # I want to be able to display ''bar'' on the contacts page >>> > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} >>> >>> Should be params=>.... I here if I recall correctly. >>> >> >> Doesn''t appear to be, replacing :data with :params changes the HTML link >> as follows: >> data-foo=''bar'' becomes params="{:foo=>"bar"}" >> >> Changing :data to params just mashes up the html link. >> >> AFAIK the generated HTML link is correct data-foo=''bar'', shouldn''t this >> be accessible in the controller as part of the params hash? >> >> If I inspect the params hash, all that is there is the usual key/value >> pairs: controller, action, (when applicable => id, method, >> authenticity_token). >> >> How can I send an additional data attribute to the controller? Do I have >> problem with my routes? >> > > pass it on the named route > > link_to ''foo'', foo_path(:foo => ''bar'', :blah => ''baz'') >Thanks Jim, that''s what I was after. I confused something I saw previously with JS (thanks to Matt (below) for jogging my memory) - probably the Railscast for Unobtrusive JS. Thanks again.> > >> >> Thanks >> >> Paul >> >> -- >> 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 rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> >> . >> To unsubscribe from this group, send email to >> rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/lN53bwOAiNoJ. >> >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.com >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/J7DyOeSBRhoJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, October 10, 2012 5:38:15 PM UTC+1, Matt Jones wrote:> > > > On Wednesday, 10 October 2012 06:25:25 UTC-7, paul h wrote: >> >> Hi Norbert, >> >> Thanks for the reply, but can you elaborate a little? >> >> On Wednesday, October 10, 2012 12:01:54 PM UTC+1, Norbert Melzer wrote: >>> >>> >>> Am 10.10.2012 09:47 schrieb "paul h" <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org>: >>> >>> > # I want to be able to display ''bar'' on the contacts page >>> > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} >>> >>> Should be params=>.... I here if I recall correctly. >>> >> >> Doesn''t appear to be, replacing :data with :params changes the HTML link >> as follows: >> data-foo=''bar'' becomes params="{:foo=>"bar"}" >> >> Changing :data to params just mashes up the html link. >> >> AFAIK the generated HTML link is correct data-foo=''bar'', shouldn''t this >> be accessible in the controller as part of the params hash? >> >> If I inspect the params hash, all that is there is the usual key/value >> pairs: controller, action, (when applicable => id, method, >> authenticity_token). >> >> How can I send an additional data attribute to the controller? Do I have >> problem with my routes? >> >> > Data-attributes are intended for code *on* the page (JS, etc) to use - > they don''t get sent to the server unless you''re doing something fancy with > JS to send them. You''ll want to pass additional parameters to the URL > helper (contacts_path in your example) to get them in the controller. >Thanks for the info Matt. Now I remember where I may have seen it used - I think it was a Railscast for Unobtrusive JS.> > --Matt Jones >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/PgdLSE7ONwUJ. For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, October 10, 2012 3:28:45 PM UTC+1, Colin Law wrote:> > On 10 October 2012 08:47, paul h <pa...-nqL44GTounsJ3nxcUk3PyQ@public.gmane.org <javascript:>> > wrote: > > Hi, > > > > rails 3.2.8 > > > > I must be missing something basic (and it''s been driving me mad) because > I > > am trying to send one additional data attribute to my controller from a > > link, but I can''t seem to get it into the params received by the > controller. > > > > I have looked around and found answers, but none seem to affect my > links. > > > > I''ve set up a dummy app with a home page and a single link to a contacts > > page - here is the code: > > > > routes.rb > > > > root :to => ''home#index'', :as => ''home'' > > resource ''contacts'' > > > > Haml Home Page View > > > > # I want to be able to display ''bar'' on the contacts page > > = link_to "Contacts", contacts_path, :data => {:foo => ''bar''} > > > > The generated html link: > > > > <a href="/contacts" data-foo="bar">contacts</a> > > > > But I don''t receive the foo attribute in the params object in the show > > action of ContactsController. > > > > If I add {:method => :post} to link_to, then it routes to the create > action > > of the ContactsController but again, foo is missing. > > > > = link_to "Contacts", contacts_path, {:method => :post, :data => {:foo > => > > ''bar''}} > > > > generates: > > > > <a href="/contacts" data-foo="bar" data-method="post" > > rel="nofollow">contacts</a> > > What do you see in development.log when you click the link. It should > show you the parameters being passed. Also if you do params.inspect > what do you see? You can put that in a view or use a debugger. >Hi Colin, That''s basically what I was doing - passing params.inspect to the view so I could see exactly was there. I wasn''t getting anything other than the standard params hash you''d expect for a simple link - controller, action, id. Thanks for the reply.> > Colin >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/eY70AMA7EAkJ. For more options, visit https://groups.google.com/groups/opt_out.