Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So for your route, the <code>:id</code> is present, right? That's why you have to provide the id when you generate your route.</p> <p>You could overwrite the route so that it contains no <code>:id</code>, or set <code>:id</code> as optional (use <code>(:id)</code>, and then append your id as param to send.</p> <p>However I haven't used remote_function before :D</p> <p>===== UPDATE =====</p> <p>So I'm really not familiar with remote_function as I seldom use the built-in prototype, but jquery.</p> <p>It seems like you are giving a list of city id for choose, then pass the id to the sever to get the city, and display it on the page.</p> <p>The following is just an example, might not suit you case:</p> <p>Define the route:</p> <pre><code>map.ajax_show_city "/cities/:id", :controller =&gt; "cities", :action =&gt; "show" </code></pre> <p>In CitiesController</p> <pre><code>def show @city = City.find(params[:id]) respond_to do |format| format.html #if you don't like, you could delete this line format.js # auto call cities/show.js.erb end end </code></pre> <p>In cities/show.js.erb</p> <pre><code>jQuery("#the_place_you_want").append("&lt;%= javascript_escape(render(:partial=&gt;@city)) %&gt;"); </code></pre> <p>The render would use cities/_city.html.erb to give the city html code</p> <pre><code>&lt;%= select_tag "cities", cities_options(with_id_as_the_option_value), :onchange =&gt; "show_city(this, '#{url_for(ajax_show_city_path)}')" %&gt; # This will generate something like: &lt;select name="cities" id="cities" onchange="show_city(this, '/cities')"&gt; # note the path generated &lt;option value="1"&gt;City 1&lt;/option&gt; &lt;option value="2"&gt;City 2&lt;/option&gt; &lt;/select&gt; </code></pre> <p>The js function needed:</p> <pre><code>function show_city(select, link) { # sorry for using jQuery $.post(link + "/" + retrieve_option_value(select), "", null, "script"); # $.post(link_to_post, addintional_data, handling_function_after_success, request_type) # This would post to /cities/2 with no any params added. # Due the the request type being set to script, the returned data would be treated as javascript and executed. So no handling function is needed (that's why null) } function retrieve_option_value(select) { $(select).children(":selected").val(); # just get the selected option's value } </code></pre> <p>That's all.</p> <p>So if the js is switched off, the user could still press the submit button (use js to hide or detach the button) and get proper result! Adding js is just adding more fancy things. Of course, I agree that almost everyone won't turn off their js support. But it's the accessibility to the disables.</p> <p>=====</p> <p>It's quite tricky that for resource's show path, it seems you must provide an <code>id</code> in order to generate the path without error.</p> <p>you could test the routes:</p> <pre><code>map.connect "/test", :controller =&gt; "cities", :action =&gt; "test" # for test only map.resources :cities map.other "/cities/show/:id", :controller =&gt; "cities", :action =&gt; "show", :method =&gt; :get def test render :text =&gt; url_for(city_path) # ambiguous routes error render :text =&gt; url_for(city_path(0) # gives /cities/0 render :text =&gt; url_for(other_path) # gives /cities render :text =&gt; url_for(other_path(0)) # gives /cities/0 end </code></pre> <p>A workaround for your way is to generate <code>/cities/0</code> and in runtime use js to replace <code>0</code> by your selected id.</p>
 

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