0

I have a search form in a modal as below:

<%= form_tag "/search", :remote => true, :method => :get do %> 
  <input type="text" name="search_name">
  <button type="submit">Search</button>
<% end %>

<div id="channels"></div>

the above form invoke below method in my controller:

#my_controller.rb:

def search
  parsed_json = JSON.parse(@json_string) # fetch some json data
  render do |index|
    ndex.html {}
    index.js {}
  end
end

so I expect the above search method to render below index.js.erb from app/view/my_controller to update my div :

$("#channels").html("<%= render :partial => "channels" %>")

after clicking the search button the view can not be updated because index.js.erb can not be invoked, any idea?

P.S: I am using rails 3.2

tokhi
  • 21,044
  • 23
  • 95
  • 105

1 Answers1

0

You are missing the 'escape_javascript' tag in your index.js.erb

Replace:

$("#channels").html("<%= render :partial => "channels" %>")

With:

$("#channels").html("<%= escape_javascript render :partial => "channels" %>")

Read more on this using: Why escape_javascript before rendering a partial?

Also, I may be wrong but this seems a bit odd:

render do |index|
  index.html {}
  index.js {}
end

Shouldn't it be:

respond_to do |format|
  format.html {}
  format.js { render 'index.js.erb' }
  #                  ^ If you dont write this, it will look for search.js.erb in your views dir
end
Community
  • 1
  • 1
Vageesh Bhasin
  • 553
  • 2
  • 12