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:

link here

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

On Jul 7, 3:12 pm, James B. [email protected]

Nicholas H. 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 B. [email protected]

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 B. wrote:
On Jul 7, 3:12�pm, James B. [email protected]

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 B. 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 tag, but
not the onclick.

Arrrgggh

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

On Jul 7, 2009, at 3:12 PM, James B. wrote:

In any case I can either get the url_for options to work or the html
onclick="if (confirm(‘Press OK’))
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”) %>

link here

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 B. http://agileconsultingllc.com
[email protected]

<%= link_to(‘Your Link’, your_controller_path, :confirm => ‘Are you
sure?’,
:method => :post, :id => “css_id”, :class => “css_class”) %>

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

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

On Jul 8, 8:50 am, James B. [email protected]

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

Matt J. 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs