I tried following the instructions in this answer for rendering json data in a .js.erb file. But I get this error:
ActionView::MissingTemplate (Missing template activities/json, application/json with {:handlers=>[:erb, :builder], :formats=>[:js, :html], :locale=>[:en, :en]}.
I have a good amount of data to send to the user hence I wanted to send as json than text/html.
Here is my controller 'some_controller.rb':
def some_action
@hash = ...
@var = ...
.
.
respond_to do |format|
format.js { render :json }
end
end
Here is my view in views/some_controller/some_action.js.erb:
$("#some_div").html("<%=j render :partial => 'some_controller/another_action' %>");
Here is my another_action.html.erb:
<%= self.formats = ["html"] %>
Var1 = <%= @var1 %>
Hash1 = <%= @hash1.inspect %>
Is the answer in the link above wrong? Can't we send json data to a js.erb file? I tried to convert the vars and hashes using .to_json. For example:
@hash1 = someMethod(someParams).to_json
But @hash1 was rendered as a string in my final html.erb which cannot be parsed, of course! Is there something I did wrong or missed?
Also there are no tutorials available online for json rendering in rails, can somebody please provide some nice links to learn and json and rails? Help is greatly appreciated. Thanks in advance.
Thank you, pR
Rendering JSON doesn't require a .js.erb file. All you need to do is
or something similar.