I have a standard devise edit user form:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<p>Current Photo</p>
<%= image_tag @user.photo.url %>
<%= f.file_field :photo %>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
Now, Rails, obviously complains because of the line: <%= image_tag @user.photo.url %> since there isn’t an @user in the edit action of the devise controller. Usually, I’d be able to just put @user = current_user in the controller and be done.
The goal is to show the user what their current photo is before they upload a new one. What’s the best way to do that?
If User is the only resource that will use devise, you could use
resource.photo.url.If there are some different classes that will authenticate with devise, but will not have a photo, you could use
resource.respond_to?(:photo)to include the photo and photo fields like this:That way, you only include the photo information when the class has a photo attribute