So I’m new to Ajax in RoR and having first problem with executing .js.erb file after some action ends. By ‘some action’ I mean not default generated by Rails, because those seem to work fine.
Here’s what I got:
-
index.html.erb:
<%= form_tag(:action => 'add_comment', :remote => true) do |c| %> Name: <%= text_field :comment, :name %> <br/> Comment: <br/> <%= text_area :comment, :text, :cols => 30, :rows => 10 %> <br/><br/> <%= submit_tag 'Add comment' %> <% end %> -
action add_comment in comments_controller.rb:
def add_comment @comment = Comment.new(params[:comment]) @comment.save respond_to do |format| format.html { redirect_to comments_path } format.js end end -
routes.rb:
match 'comments/add_comment' => 'comments#add_comment'
And yet comment is added by refreshing page – add_comment.js.erb is not executed (when renaming both action add_comment and add_comment.js.erb to ‘create’ works).
What am I doing wrong?
In your routes: