Rails:未定义的方法'MODELNAME'?我在创建时保存了错误的东西吗?

时间:2012-10-11 19:00:33

标签: ruby-on-rails

目前,我正在为用户制作应用以创建相册。问题在于每个专辑可以有多个所有者,因此在创建专辑的表单中有一个复选框部分,您可以在其中突出显示您的朋友姓名并“邀请他们”成为所有者。但是,由于它在我的AlbumsController#create中给了我一个错误,因此我很难将其付诸实践。错误是:

undefined method 'user' for #<Album:0x007fcd9021dd00>

app/controllers/albums_controller.rb:43:in 'create'

这是我的表格

<%= form_for ([@user, @album]), :html => { :id => "uploadform" } do |f| %>
<div class="formholder">
    <%= f.label :name %>
    <%= f.text_field :name %>

    <br>
    <label>Hosts</label>
    <% @user.friends.each do |friend| %>
    <%= friend.name %>
    <%= check_box_tag 'album[user_ids][]', friend.id, @album.users.include?(friend) %>
    <% end %>

    <%= f.label :description %>
    <%= f.text_area :description %>

    <br>

    <%=f.submit %>
</div>
<% end %>

复选框返回一个friend.id值数组,我想邀请它们作为相册的所有者。这里棘手的部分是我在join album_user模型中有一个虚构的pending_album列。这是什么给我发错,我无法弄清楚如何解决:

专辑控制器

def create
  @user = User.find(params[:user_id])
  @album = @user.albums.build(params[:album], :status => 'accepted')
  @friends = @user.friends.find(params[:album][:user_ids])
  for friend in @friends
    params[:album1] = {:user_id => friend.id, :album_id => @album.id, :status => 'pending'}
    AlbumUser.create(params[:album1])
  end
      #the next line is where the error occurs. why???
  if @user.save
    redirect_to user_album_path(@user, @album), notice: 'Album was successfully created.'
  else
    render action: "new"
  end
end

专辑模型

class Album < ActiveRecord::Base
  attr_accessible :name, :description, :user_ids
  validates_presence_of :name

  validates :user, :uniqueness => true

  has_many :album_users
  has_many :users, :through => :album_users
  has_many :photos

end

用户模型

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, :conditions => "status = 'accepted'"
has_many :pending_albums, :through => :album_users, :source => :album, :conditions => "status = 'pending'"
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

album_user连接表模型

class AlbumUser < ActiveRecord::Base
  attr_accessible :user_ids, :album_id, :status, :user_id
  belongs_to :album
  belongs_to :user
end

参数(看起来有点可疑......特别是因为album_id是零):

{ “UTF8”=&gt; “中✓”,  “authenticity_token”=&gt; “中xkIi6 + 1vjEk4yQcFs9vI1uvI29 + Gyuenyp71vhpX9Hw =”,  “专辑”=&GT; { “名称”=&gt; “中123123”,  “user_ids”=&GT; [ “27”,  “28”],  “描述”=&gt; “中123123”},  “commit”=&gt;“创建相册”,  “USER_ID”=&gt; “中29”,  “album1”=&GT; { “USER_ID”=&GT; 28  “album_id”=&GT;零,  “状态”=&gt; “中未决的”}}

我不太确定我做错了什么。请有人帮忙!

2 个答案:

答案 0 :(得分:1)

您的相册真的有很多用户吗?那应该返回方法“用户”?如果想要一个用户拥有相册,则必须在模型中使用belongs_to。然后方法“user”将正确的用户返回到相册

答案 1 :(得分:1)

错误消息是:

undefined method 'user' for #<Album:0x007fcd9021dd00>

好吧,所以有人打电话给Album#user。控制器中的第46行是@user.save,那么如何调用user

class Album < ActiveRecord::Base
  attr_accessible :name, :description, :user_ids
  validates_presence_of :name

  validates :user, :uniqueness => true    # <-- bam!

  has_many :album_users
  has_many :users, :through => :album_users
  has_many :photos
end

save触发验证,包括那里的那一行,验证user是唯一的。由于这是爆炸性的,我猜测user是一个不存在的列,或者因为a)它没有在迁移中定义,或者b)你没有运行迁移。

(顺便说一句,我很担心为什么你会在user上拥有唯一的Album属性,AlbumUser模型同时包含user_id和{ {1}}和user_ids模型。也许它有意义,但我认为它更有可能需要清理。)