Rails form_tag remote example

后端 未结 3 1277
轻奢々
轻奢々 2020-12-31 18:55

I have this incredible simple form:

<%= form_tag(\"/portal/search\", method: \'get\', remote: true ) do %>

  <%= label_tag(:query, \'Search for:\')         


        
3条回答
  •  难免孤独
    2020-12-31 19:43

    Let's say you get @results out of your search method.

    In the controller, search method:

    respond_to do |format|
      format.html {render or redirect_to wherever you need it}
      format.js 
    end
    

    Then all you need is a file named search.js.erb to run the required js at the end of your request. It might look something like:

    $('#results_div').html("<%= j @results.pluck(:title).join('
    ') %>")

提交回复
热议问题