Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COWell then what are you using to make remote calls from a page? This seems like something very common that any Rails app would be doing. It's nothing fancy, but I'm having a hard time keeping it RESTful and making a remote call... I dont want link_to_remote, and I dont want to submit a remote form. What are my other options? Do I need to move to JQuery and Uobtrusive Javascript? I can't find good info on this with UJS and JQuery, can you point me towards a good link?
      singulars
    2. COActually I am using jQuery. I usually built the site without any AJAX call first so that it works even when javascript was disabled or screen readers. After that I use javascript to make form submit uses AJAX (so won't be affected if javascript disabled). Making a form to submit by AJAX could simply by attaching generic a function to onsubmit event. `$(".turn_me_to_ajax").live("submit", submit_with_ajax);` and `function submit_with_ajax() { $.post($(this).attr("action"), $(this).serialize(), null, "script"); }`. Simple ^^
      singulars
    3. COFirst of all, I dont care about users with javascript disabled. If they dont have JS, then they might as well not even bother trying to do anything on the internet. And I dont care about screen readers. You are describing a remote form submission with JQuery, which is not what I am after. Of course I know how to submit a remote form, it is simple. I need to call a remote function from a page. If you aren't doing this, then your sites must be very boring and simplistic!
      singulars
 

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