I have a function on my rails app where a user can add a favourite by pressing a button. Once the favourite is added, the page reloads to the top of the screen. The guideline that the user has favourited could be half way down the screen and it's annoying for them to then have to start viewing at the top again. Is there a way for it just to be changed to 'favourite' but stay where the user is on the page?
In index.html.erb (views)
<% if !current_user.favourite_guidelines.select{|f| f.guideline_id==guideline.id}.any? %>
<%= link_to guidelines_favourite_path(guideline_id: guideline.id, type: "favourite"), method: "get", class: "btn btn-small main" do%>
<i class="icon icon-star"></i> Favourite
<% end %>
<% end %>
<% if current_user.favourite_guidelines.select{|f| f.guideline_id==guideline.id}.any? %>
<%= link_to guidelines_favourite_path(guideline_id: guideline.id, type: "unfavourite"), method: "get", class: "btn btn-small main" do%>
<i class="icon-star-empty"></i> Unfavourite
<% end %>
guidelines_controller.rb
def favourite
type = params[:type]
if type == "favourite"
@guideline= Guideline.find_by_id(params[:guideline_id])
current_user.favourite_guidelines.build(:guideline_id => @guideline.id).save
redirect_to :back
elsif type == "unfavourite"
@guideline= Guideline.find_by_id(params[:guideline_id])
current_user.favourite_guidelines.where(:guideline_id => @guideline.id).destroy_all
redirect_to :back
else
# Type missing, nothing happens
redirect_to :back, notice: 'Nothing happened.'
end
end