Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should try something like the following:</p> <pre><code>link_to "Example 1", "http://www.example.com", :confirm =&gt; needs_confirmation? ? "Are you sure?" : nil </code></pre> <p>And for the second case you could in a similar say the following:</p> <pre><code>link_to "Example 2", "http://www.example.com", :id=&gt;unique_link_id </code></pre> <p>Note: unique_link_id should return a string (or nil if you dont want to have an id)</p> <p>To answer your question in general you need to understand how rails treats these parameters that you pass in a method (such as a helper).</p> <p>Most rails helpers start like this:</p> <pre><code>def a_helper(*args) options = args.extract_options! ... end </code></pre> <p>What you should know about the above: It is assumed that an Array is passed in as parameter. All of the :something=>value that you also add are converted into a hash which is the last element of this array. So <code>extract_options!</code> separates the normal parameters from the hash options. So in order what you want to work, <em>all</em> options should be in <em>one</em> hash in the end.</p> <p>To make a long story sort here is an example of what you should do:</p> <pre><code> link_to "Example 1", "http://www.example.com",{:confirm=&gt;"Are you sure?"}.merge(unique_link_id) </code></pre> <p>In this case <code>unique_link_id</code> returns a hash and is merged with the other options. If <code>unique_link_id</code> returns an empty hash(<code>{}</code>) nothing is added to your options and everyone is happy :) </p> <p>If your options get too complicated i suggest that you create an options variable before calling the helper (or method) you wish. <code>options</code> should be a hash merged with other hashes as many times as you wish before you pass it as the last parameter to your method. </p> <p>Tip: very useful hash functions are hash.merge(), hash.merge!() and hash.reverse_merge()</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload