Reputation: 2020
I'm having issues getting an instance variable to show up in my views. I'm setting it using a private method, and I'm using a before_filter to ensure that I can access it in the two places I need it. However, the instance variable is not showing up in the view, let alone having the needed action.
In my controller I have
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_filter :set_form, :only => [ :edit_password, :edit_email ]
...
def edit_password
@user = current_user
set_form('password')
end
def edit_email
@user = current_user
set_form('email')
end
...
private
def set_form(edit_type)
@form = edit_type
end
Here is the view file
<h1>Edit Your Account</h1>
<title><%= @form %></title>
<% if @form == 'email' %>
<%= render 'edit_email_form' %>
<% else %>
<%= render 'edit_password_form' %>
<% end %>
<%= link_to 'Back', user_path(@user) %>
I'm not sure what else I need to do to be able to use the instance variable in the view. I'm also wondering whether it's still possible to access the instance variable in the update statement after? Any help would be appreciated!
Upvotes: 0
Views: 1261
Reputation: 36860
There's no value in setting set_form
in a before_filter, as you're calling it in your methods.
You need to explicitly render your view file... I don't see you're doing that. If the form is called (for example) edit_value.html.erb
then your two edit_...
methods need to have
render :edit_value
Instance values are not available in the update statement. When you update nothing is available except the information submitted in your form or url, or anything you saved in the session
hash, or anything you persisted to your database.
Upvotes: 1