bigpotato
bigpotato

Reputation: 27547

Rails: undefined method '(MODELNAME)'?

Hi I'm currently making a web app for uploading pics. There are users, albums, and photos. I'm having trouble trying to figure out what's going on when I try to edit a user's profile information. It is telling me that there is an undefined method 'album' NoMethodError in UsersController#update , but I don't even call album in my #update action.

def update
  respond_to do |format|
    if current_user.update_attributes(params[:user])
      format.html { redirect_to current_user, notice: 'User successfully updated.'}
      format.json { render json: current_user, status: :created, location: current_user }
    else
      format.html { render action: 'edit' }
      format.json { render json: current_user.errors, status: :unprocessable_entity }
    end
  end
end

the error is specifically on the 2nd and 3rd lines of my update action. Here's the error:

app/controllers/users_controller.rb:45:in 'block in update'

app/controllers/users_controller.rb:44:inupdate'`

anyone know what the heck is going on?

edit form for users

<%= form_for(@user) do |f| %>

<div class="formhold">
<div class="field">
    <% if @user.profilepic? %>
    <%= image_tag @user.profilepic.url %>
    <% end %>

    <%= f.label :profilepic, "Profile Picture" %>
    <%= f.file_field :profilepic %>
</div>
<div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
</div>
<div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
</div>

<div>
    <%= f.submit 'Submit', :class => 'submitbutton' %>
</div>



<% if @user.errors.any? %>
    <div id="error_exp">
        <h2><%= pluralize(@user.errors.count, "error") %> occurred.</h2>

        <ul>
        <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
        </ul>
    </div>

    </div>
<% end %>
<% end %>



<%= link_to "Back", user_path(@user) %>

user model

class User < ActiveRecord::Base

has_secure_password
attr_accessible :email, :name, :password, :password_confirmation, :profilepic
validates_presence_of :password, :on => :create

validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create
validates :album, :uniqueness => true

has_many :album_users
has_many :albums, :through => :album_users
accepts_nested_attributes_for :albums

has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at

has_attached_file :profilepic

before_save { |user| user.email = email.downcase }

def name_with_initial
  "#{name}"
end

private

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

end

Upvotes: 0

Views: 208

Answers (1)

iouri
iouri

Reputation: 2929

Try commenting out:

validates :album, :uniqueness => true

It is assuming that :album is an attribute/method of user and failing. You should handle your unique validations for albums in albums model, depending on your business logic. You can validate them based on user_id or some other attribute with :scope => [:user_id]

Upvotes: 1

Related Questions