I have a page with a form I'd like to see the results of without reloading in my rails app, but I'm new to incorporating JS in ruby on rails. I've tried following along Display result data without page refresh on form submission in ruby on rails as well as a few 'intro to AJAX' guides, but am struggling to apply them to my own project.
In my user profile profile.html.erb
, I have a list of tags for a review
<% if review.tags.length > 0 %>
<% review.tags.each do |tag| %>
<%= tag.category %>
<%= tag.name %>
<% end %>
<% end %>
Under which there's a form to add a new tag
<%= simple_form_for @tag, url: "/add_tag/#{review.id}" do |form| %>
<%= form.input :category %>
<%= form.input :name %>
<%= form.button :submit %>
<% end %>
(I'm currently using the gem simple_form for formating reasons. It's not the cause of my confusion, though I think I might need to remove it to apply a solution)
This form submits to my reviews_controller
, which has this method add_tag
def add_tag
@review = Review.find_by(id: params[:id])
tag_cat = params[:tag][:category]
tag_name = params[:tag][:name]
if valid_tag(tag_cat, tag_name) && tag_cat != ""
@tag = grab_tag(tag_cat, tag_name)
if ReviewTag.find_by(tag_id: @tag.id, review_id: @review.id).nil?
ReviewTag.create(tag_id: @tag.id, review_id: @review.id)
end
redirect_to '/profile'
else
redirect_to '/profile'
end
end
The redirect_to /profile
works just fine at the moment, though it seems I need to use format.js
in some way instead? I'm struggling to see what changes I need to make on the javascript and view side.