Note that there are some explanatory texts on larger screens.

plurals
  1. PORJS: Using observe_field on select_tag
    primarykey
    data
    text
    <p>The dropdown-menu (built by the select_tag) in my application should call the filter-category-action as soon as the user changes the value in the dropdown-menu AND hits the 'Go' button. </p> <p>Now I'd like to get rid of the 'Go' button and have an observer (observe_field?) call the filter-category-action as soon as the user changes the value in the dropdown-menu.</p> <p>Below you see the code I've written. It works using the 'Go'-Button but doesn't work by just changing the value in the dropdown-menu. What's wrong with my observe_category_select-helper?</p> <p><strong>View-partial with dropdown-menu and project list</strong></p> <pre><code> &lt;!-- drop down menu --&gt; &lt;% form_tag(filter_category_path(:id), :method =&gt; :post, :class =&gt; 'categories') do %&gt; &lt;label&gt;Categories&lt;/label&gt; &lt;%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, @category_id)) %&gt; &lt;!-- i would like to get rid of this button --&gt; &lt;%= submit_tag "Go" %&gt; &lt;% end %&gt; &lt;!-- list of projects related to categories chosen in drop down menu --&gt; &lt;ul class="projects"&gt; &lt;% @projects.each do |_project| %&gt; &lt;li&gt; &lt;%= link_to(_project.name, _project) %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;%= observe_category_select -%&gt; </code></pre> <p><strong>HelperMethod</strong></p> <pre><code> def observe_category_select observe_field( :category, :url =&gt; filter_category_path(:id), :with =&gt; 'category', :on =&gt; 'change' ) end </code></pre> <p><strong>Javascript-Output of the HelperMethod</strong></p> <pre><code>&lt;script type="text/javascript"&gt; //&lt;![CDATA[ new Form.Element.EventObserver('category', function(element, value) { new Ajax.Request('/categories/id/filter', {asynchronous:true, evalScripts:true, parameters:'category=' + encodeURIComponent(value) + '&amp;authenticity_token=' + encodeURIComponent('edc8b20b701f72285068290779f7ed17cfc1cf8c')}) }, 'change') //]]&gt; &lt;/script&gt; </code></pre> <p><strong>Categories controller</strong></p> <pre><code>class CategoriesController &lt; ApplicationController def show @category = Category.find(params[:id]) @category_id = @category.id @projects = @category.projects.find(:all) respond_to do |format| format.html # index.html.erb end end def index @projects = Category.find(params[:id]).projects.find(:all) respond_to do |format| format.html # index.html.erb end end def filter @category = Category.find(params[:category]) @category_id = @category.id @projects = @category.projects.find(:all) respond_to do |format| format.html # index.html.erb end end </code></pre> <p>end</p> <p><strong>Output of 'rake routes | grep filter'</strong></p> <pre><code> filter_category POST /categories/:id/filter {:controller=&gt;"categories", :action=&gt;"filter"} formatted_filter_category POST /categories/:id/filter.:format {:controller=&gt;"categories", :action=&gt;"filter"} </code></pre>
    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.
 

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