Rails: How to pass value from one action to another in the same controller

ruby-on-rails

Solution

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

Problem

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 ```

Original source

Related problems