Reputation: 35
I've got multiple link_to methods in my view. Using the remote option true is there any way I can tell which link was clicked in my controllers update action?
Also, specifically I would like my link_to methods to call the update action in my controller which will increment or decrement the instance variable @lang, save the value in the DB, and then redirect back to the show action.
Here is what I have so far in my view file:
<div class="center hero-unit">
<%= @word.english %>, <%= @word.english_to_spanish %>
<%= link_to "Previous", "/langs/#{@lang - 1}", :method => :put %>
<%= link_to "Next", "/langs/#{@lang + 1}", :method => :put %>
</div>
Here is my langs controller:
class LangsController < ApplicationController
def show
@word = Lang.find(params[:id])
@lang = current_user.bookmark #bookmark keeps track in db for what word user is on
end
def update
@lang = @lang - 1
current_user.bookmark = @lang
current_user.save
render 'show'
end
end
Thanks for the help in advance!
Upvotes: 0
Views: 573
Reputation: 1089
To determine which link was clicked on your controllers update action, you can pass optional parameters:
<%= link_to "Previous", user_path(:id => @lang - 1, :option => 'previous'), :method => :put%>
<%= link_to "Next", user_path(:id => @lang + 1, :option => 'next'), :method => :put%>
Then, in your controller check params[:option]
value.
Q: would the syntax in the controller be something like if params[:next] or something like if params[:option] == "next")
Actually, params[:option] == "next"
is what you need to check the clicked link.
But if you prefer hipster-code style (like I am), you can do a trick:
class UsersController < ApplicationController
..
def update
if link_to(:next)
#'Next' link was clicked
end
end
def link_to(option)
params[:option] == option.to_s
end
Upvotes: 0