link_to, how do you combine url options and html_options?

View: New views
12 Messages — Rating Filter:   Alert me  

link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Nicholas Henry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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...@...>
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/.
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Rob Biedenharn-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



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


--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Nicholas Henry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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<Rob@...> 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
> Rob@...
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Arzumy MD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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...@...> 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...@...> 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...@...
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


<%= 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/.

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Matt Jones :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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...@...>
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/.
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: link_to, how do you combine url options and html_options?

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---