James Byrne
2009-Jul-07 19:12 UTC
link_to, how do you combine url options and html_options?
I have a link-to that I wish to look somewhat like this: link_to "link here", new_object_path, :method => :post, :confirm => ''Press OK'', #url options :class => "css_class", :id => "css_id" #html options In any case I can either get the url_for options to work or the html options to work, but no matter how I arrange () or {} I cannot get both to work together. As far as I can determine, the api documents do not explicitly show this case either. What I want link_to to generate is: <a href="/objects/new" class="css_class" id="css_id" onclick="if (confirm(''Press OK'')) { var f = document.createElement(''form''); f.style.display = ''none''; this.parentNode.appendChild(f); f.method = ''POST''; f.action = this.href; f.submit(); }; return false;">link here</a> How is this done? -- Posted via http://www.ruby-forum.com/.
Nicholas Henry
2009-Jul-07 19:37 UTC
Re: link_to, how do you combine url options and html_options?
Based on the documentation: link_to(name, options = {}, html_options = nil) I would expect this is what you are looking for (note haven''t tested it): link_to "link here", {new_object_path, :method => :post, :confirm => ''Press OK''), #url options :class => "css_class", :id => "css_id" #html options HTH, Nicholas On Jul 7, 3:12 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a link-to that I wish to look somewhat like this: > > link_to "link here", new_object_path, > :method => :post, :confirm => ''Press OK'', #url > options > :class => "css_class", :id => "css_id" #html > options > > In any case I can either get the url_for options to work or the html > options to work, but no matter how I arrange () or {} I cannot get both > to work together. As far as I can determine, the api documents do not > explicitly show this case either. > > What I want link_to to generate is: > > <a href="/objects/new" > class="css_class" > id="css_id" > onclick="if (confirm(''Press OK'')) > { > var f = document.createElement(''form''); > f.style.display = ''none''; > this.parentNode.appendChild(f); > f.method = ''POST''; > f.action = this.href; > f.submit(); > }; > return false;">link here</a> > > How is this done? > -- > Posted viahttp://www.ruby-forum.com/.
James Byrne
2009-Jul-07 19:52 UTC
Re: link_to, how do you combine url options and html_options?
Nicholas Henry wrote:> Based on the documentation: > > link_to(name, options = {}, html_options = nil) > > I would expect this is what you are looking for (note haven''t tested > it): > > link_to "link here", {new_object_path, > :method => :post, :confirm => ''Press OK''), #url > options > :class => "css_class", :id => "css_id" #html > options > > HTH, > Nicholas > > On Jul 7, 3:12�pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Presuming that you actually meant the closing ) to be a } I had already tried that. This is what happens: <%=link_to(''link here'', { new_object_path, :method => :post, :confirm => "Press OK"}, :id => "css_id") %> compile error index.html.erb:86: syntax error :method => :post, ^ index.html.erb:87: syntax error :confirm => "Press OK"}, index.html.erb:89: syntax error ).to_s); @output_buffer.concat "\n" ^ -- Posted via http://www.ruby-forum.com/.
James Byrne
2009-Jul-07 19:58 UTC
Re: link_to, how do you combine url options and html_options?
James Byrne wrote: On Jul 7, 3:12�pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > Presuming that you actually meant the closing ) to be a } I had already > tried that. This is what happens: > > > <%=link_to(''link here'', > { new_object_path, > :method => :post, > :confirm => "Press OK"}, > :id => "css_id") > %> > >But we were sooooo close. The actual code has to be this: <%=link_to(''link here'', { new_object_path, { :method => :post, # note hash within block :confirm => "Press OK" } }, :id => "css_id") %> Obscure does not do this sort of thing justice. -- Posted via http://www.ruby-forum.com/.
James Byrne
2009-Jul-07 20:02 UTC
Re: link_to, how do you combine url options and html_options?
James Byrne wrote:> But we were sooooo close. The actual code has to be this:That code does not work either. It does not generate an error but now I am back to having the id and class attributes set in the <a> tag, but not the onclick. Arrrgggh -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2009-Jul-07 20:33 UTC
Re: link_to, how do you combine url options and html_options?
On Jul 7, 2009, at 3:12 PM, James Byrne wrote:> > I have a link-to that I wish to look somewhat like this: > > > link_to "link here", new_object_path, > :method => :post, :confirm => ''Press OK'', #url > options > :class => "css_class", :id => "css_id" #html > options > > In any case I can either get the url_for options to work or the html > options to work, but no matter how I arrange () or {} I cannot get > both > to work together. As far as I can determine, the api documents do not > explicitly show this case either. > > What I want link_to to generate is: > > <a href="/objects/new" > class="css_class" > id="css_id" > onclick="if (confirm(''Press OK'')) > { > var f = document.createElement(''form''); > f.style.display = ''none''; > this.parentNode.appendChild(f); > f.method = ''POST''; > f.action = this.href; > f.submit(); > }; > return false;">link here</a> > > How is this done?What''s wrong with keeping it simple? <%= link_to(''link here'', "/url", :method => :post, :confirm => "Press OK", :id => "css_id", :class => "css_class") %> <a href="/url" class="css_class" id="css_id" onclick="if (confirm(''Press OK'')) { var f = document.createElement(''form''); f.style.display = ''none''; this.parentNode.appendChild(f); f.method = ''POST''; f.action = this.href;var s = document.createElement(''input''); s.setAttribute(''type'', ''hidden''); s.setAttribute(''name'', ''authenticity_token''); s.setAttribute(''value'', ''1R/ dpXEMn3tUvmXCbVQtdHAnSw9YU36T9n2lDtZ8WlQ=''); f.appendChild(s);f.submit(); };return false;">link here</a> Surely, you can also replace "/url" with a named route as in your original example. Did you say what version of Rails you have? My example uses 2.3.2. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Arzumy
2009-Jul-08 02:03 UTC
Re: link_to, how do you combine url options and html_options?
If this is taking too much of your time, why don''t just wrap your link in a span or div then control the class from there? Just a thought. And I can confirm this works in 2.3.2 too, as per Nicholas post link_to "link here", new_object_path, :method => :post, :confirm => ''Press OK'', :class => "css_class", :id => "css_id" Cheers! Arzumy On Jul 8, 6:21 am, Nicholas Henry <nicholas.he...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> OK, James, I have tested the following in Rails 2.3.2: > > link_to "link here", new_object_path, :method => :post, :confirm => > ''Press OK'', :class => "css_class", :id => "css_id" > > and this does work as you would expect. > > What version are you on? > > On Tue, Jul 7, 2009 at 4:33 PM, Rob > > Biedenharn<R...-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote: > > > On Jul 7, 2009, at 3:12 PM, James Byrne wrote: > > >> I have a link-to that I wish to look somewhat like this: > > >> link_to "link here", new_object_path, > >> :method => :post, :confirm => ''Press OK'', #url > >> options > >> :class => "css_class", :id => "css_id" #html > >> options > > >> In any case I can either get the url_for options to work or the html > >> options to work, but no matter how I arrange () or {} I cannot get > >> both > >> to work together. As far as I can determine, the api documents do not > >> explicitly show this case either. > > >> What I want link_to to generate is: > > >> <a href="/objects/new" > >> class="css_class" > >> id="css_id" > >> onclick="if (confirm(''Press OK'')) > >> { > >> var f = document.createElement(''form''); > >> f.style.display = ''none''; > >> this.parentNode.appendChild(f); > >> f.method = ''POST''; > >> f.action = this.href; > >> f.submit(); > >> }; > >> return false;">link here</a> > > >> How is this done? > > > What''s wrong with keeping it simple? > > > <%= link_to(''link here'', "/url", :method => :post, :confirm => "Press > > OK", :id => "css_id", :class => "css_class") %> > > > <a href="/url" class="css_class" id="css_id" onclick="if > > (confirm(''Press OK'')) { var f = document.createElement(''form''); > > f.style.display = ''none''; this.parentNode.appendChild(f); f.method > > ''POST''; f.action = this.href;var s = document.createElement(''input''); > > s.setAttribute(''type'', ''hidden''); s.setAttribute(''name'', > > ''authenticity_token''); s.setAttribute(''value'', ''1R/ > > dpXEMn3tUvmXCbVQtdHAnSw9YU36T9n2lDtZ8WlQ=''); > > f.appendChild(s);f.submit(); };return false;">link here</a> > > > Surely, you can also replace "/url" with a named route as in your > > original example. Did you say what version of Rails you have? My > > example uses 2.3.2. > > > -Rob > > > Rob Biedenharn http://agileconsultingllc.com > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Älphä Blüë
2009-Jul-08 03:43 UTC
Re: link_to, how do you combine url options and html_options?
<%= link_to(''Your Link'', your_controller_path, :confirm => ''Are you sure?'', :method => :post, :id => "css_id", :class => "css_class") %> -- Posted via http://www.ruby-forum.com/.
James Byrne
2009-Jul-08 12:50 UTC
Re: link_to, how do you combine url options and html_options?
Älphä Blüë wrote:> <%= link_to(''Your Link'', your_controller_path, :confirm => ''Are you > sure?'', :method => :post, :id => "css_id", :class => "css_class") %>Thank you to all who replied. I am on Rails 2.3.2 and yes, the code above does work for me as well. I cannot gather from the API documents that this should work however, which is why I never tried it. -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-Jul-08 18:38 UTC
Re: link_to, how do you combine url options and html_options?
I think what was confusing you was the difference between url_options and html_options. They are not named in the most descriptive format, but the source is much clearer. The only things that end up in url_options are things that you can pass to url_for: so neither :method nor :confirm belong there. You''d pass a hash in that position if you were, for instance, using old-style routes (passing :controller and :action explicitly). All the stuff that ends up producing HTML bits ends up in the last option. The example that I think several of the previous posters were thinking of (splitting the two arrays with {}) is typically encountered with form_for; but note that form_for takes URL parameters with the :url key. --Matt Jones On Jul 8, 8:50 am, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Älphä Blüë wrote: > > <%= link_to(''Your Link'', your_controller_path, :confirm => ''Are you > > sure?'', :method => :post, :id => "css_id", :class => "css_class") %> > > Thank you to all who replied. I am on Rails 2.3.2 and yes, the code > above does work for me as well. I cannot gather from the API documents > that this should work however, which is why I never tried it. > -- > Posted viahttp://www.ruby-forum.com/.
James Byrne
2009-Jul-09 12:58 UTC
Re: link_to, how do you combine url options and html_options?
Matt Jones wrote:> > The example that I think several of the previous posters were thinking > of (splitting the two arrays with {}) is typically encountered with > form_for; but note that form_for takes URL parameters with the :url > key.I think that this is what happened to me. I encountered a similar hell when trying (eventually successfully) to get html attributes properly assigned to a select method inside a forms_for method. Given the similarity of usage and signatures, I extrapolated that (unpleasant) previous experience into this method as well. -- Posted via http://www.ruby-forum.com/.