简体   繁体   中英

How to pass variable from javascript to view in rails?

When I click the submit button, I am rendering with json in controller.

My json is {notes: array[1], array[2]} . Now I am rendering view.html.erb file . I want to show the values in notes like <%= notes %> in this file, how to display it?

If notes is an arary you can loop over it and display the content in your .erb.html file as below:

 <div> <% notes.each_with_index do | note, index | %> <div id="title"> <%= note[:title] %> </div> <% end %> </div>

sounds like you are confusing JSON requests with HTML request.

The incoming request should be a HTML

controller

# GET /the_controller/view
# not /the_controller/view.json <- wrong
def view
   @notes = array
end    

view.html.erb

<%= @notes %>

see @rishabh0211 answer for better presentation

if indeed you intend this to be a json request, through som ajax request or similiar, you need to specify that in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM