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?

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

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...@andreas-s.net>

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"                                    ^

James Byrne wrote: On Jul 7, 3:12�pm, James Byrne <rails-mailing-l...@andreas-s.net>

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.

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

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@AgileConsultingLLC.com

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?

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

<%= link_to('Your Link', your_controller_path, :confirm => 'Are you sure?',   :method => :post, :id => "css_id", :class => "css_class") %>

Ä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.

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