0

我在我的 Rails 应用程序中使用设计进行身份验证,并在我的一个控制器中使用“current_user”方法。从那时起,我在我的用户表和活动表之间建立了一个“has_and_belongs_to_many”关联,如下所示。

我的问题是在我的活动控制器中调用新/创建方法时出现以下错误:

'undefined method `user_id' for #<Activity:0x007fe89c4e8400>'

有人知道这是由什么引起的吗?谢谢!

活动控制器.rb

def create
    @activity = Activity.new(params[:activity])

    respond_to do |format|
      if @activity.save
        format.html { redirect_to @activity, notice: 'Activity was successfully created.' }
        format.json { render json: @activity, status: :created, location: @activity }
      else
        format.html { render action: "new" }
        format.json { render json: @activity.errors, status: :unprocessable_entity }
      end
    end
  end

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name

  # attr_accessible :title, :body

  #many to many association
  has_and_belongs_to_many :activities
end

活动.rb

class Activity < ActiveRecord::Base
  attr_accessible :description, :image


  validates :description, presence: true
  validates :user_id, presence: true
  validates_attachment :image, presence: true,
                        content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/gif', 'image/png']},
                        size: { less_than: 5.megabytes }
  has_and_belongs_to_many :user
  has_attached_file :image, styles: { small: "240x180"}

end

架构.rb

ActiveRecord::Schema.define(:version => 20130630004839) do

  create_table "activities", :force => true do |t|
    t.string   "description"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "activities_users", :id => false, :force => true do |t|
    t.integer  "activity_id"
    t.integer  "user_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
    t.string   "name"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end
4

1 回答 1

1

你必须这样做:

 @activity = current_user.activities.build(params[:activity])
于 2013-06-30T07:50:14.180 回答