Reputation: 768
I get input from first action, how can I pass the value to another action?
example_controller.rb
def first
@first_value = params[:f_name]
end
def second
@get_value = @first_value
end
Upvotes: 6
Views: 8659
Reputation: 52268
In addition to Steve's answer, I just wanted to point out another way is to use the session
.
In controller:
session[:your_variable] = "A Great Title!"
then access it in the view:
<h1><%= session[:your_variable] %></h1>
More reading here, here, and here.
Upvotes: 0
Reputation: 36860
You can't pass instance variables, because the second action is running in a new instance of the controller. But you can pass parameters...
def first
@first_value = params[:f_name]
redirect_to second_action_path(passed_parameter: params[:f_name])
end
def second
@first_value = params[:passed_parameter]
@get_value = @first_value
end
You can also use session
variables which is how you typically store values for a user... Don't store entire objects just keys as session store is typically limited
def first
@first_value = params[:f_name]
session[:passed_variable] = @first_value
end
def second
@first_value = session[:passed_variable]
@get_value = @first_value
Upvotes: 8