0

目前我正在为用户制作一个应用程序来创建相册。问题是每个相册可以有多个所有者,因此在创建相册的表单中有一个复选框部分,您可以在其中突出显示您的朋友姓名并“邀请他们”成为所有者。但是,我很难让它工作,因为它在我的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值,我想邀请这些值成为专辑的所有者。这里棘手的部分是pending_album我的 join album_user 模型中有一个假想的列。以下是我无法弄清楚如何修复的错误:

专辑控制器

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"=>"✓", "authenticity_token"=>"xkIi6+1vjEk4yQcFs9vI1uvI29+Gyuenyp71vhpX9Hw=", "专辑"=>{"name"=>"123123", "user_ids"=>["27", " 28"], "description"=>"123123"}, "commit"=>"创建相册", "user_id"=>"29", "album1"=>{"user_id"=>28, "album_id"= >无,“状态”=>“待定”}}

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

4

2 回答 2

1

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

于 2012-10-11T19:13:26.343 回答
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) 你没有运行迁移。

(As an aside, I'm rather confused as to why you would have a unique user attribute on Album, an AlbumUser model with both user_id and user_ids, and a User model. Maybe it makes sense, but I think it's more likely that it all needs a cleanup.)

于 2012-10-11T19:14:16.547 回答