Tuesday, 15 February 2011

javascript - Rails 4 ajax-request with form_tag -



javascript - Rails 4 ajax-request with form_tag -

i'm developing simple web-interface service, interracts database , has possibility filter items parameters. here illustration of code:

config/routes.rb

resources :list_items collection 'filter' end end

app/views/list_items/index.html.erb

<%= form_tag(filter_list_items_path, method: 'get') %> <%= text_field_tag(:some_parameter) %> <%= submit_tag 'filter', remote: true %> <% end %> <table> <%= render @list_items %> </table>

partial app/views/list_items/_list_item.erb

<tr class="<%= cycle('list_line_odd', 'list_line_even') %>" id="listelement"> <td><%= list_item.attributes[:path] %></td> </tr>

app/controllers/list_items_controller.rb

def filter @list_items ||= filter_items(params) #get @list_items array, according params respond_to |format| format.js end end

app/views/list_items/filter.js.erb

$('#listelement').html('<%= escape_javascript render(@list_items) %>');

when input info text_field , press 'filter' button, calls filter action in list_items controller. expect table content refreshed, instead of throws error actioncontroller::unknownformat.

p.s. when add together line format.html { redirect_to list_items_url } within respond_to block, doesn't throw errors , not apply filter.

i'm confused rails, please clarify i'm doing wrong. thanks

this happening because form submitting html format default format in rails, thats why format.html responding since returns html not javascript nil happens.

you getting unknownformat error because in action, accepting format.js in respond_to block.

to prepare this, seek changing form following:

<%= form_tag(filter_list_items_path(format: :js), method: 'get', remote: true) %> <%= text_field_tag(:some_parameter) %> <%= submit_tag 'filter' %> <% end %>

in above code telling rails explicitly submit form js format, generate route .js postfix, instead of putting remote indicator on submit button, right way have on form tag.

javascript ruby-on-rails ajax

No comments:

Post a Comment