在控制器中设置数据库中的值

时间:2015-01-03 19:15:38

标签: ruby-on-rails ruby-on-rails-4

我试图在下面的代码中设置电子邮件值的值但是我一直在"未定义的方法`email =' &#34 ;. current_user.email基于当前登录的用户。登录系统是使用devise进行的。永远不会有人在没有登录的情况下创建新帖子,因此总会有current_user.email值。

def create
    @post= Post.new(post_params)
    @post.email= current_user.email #this is the line where it falls apart
    if @post.save
        redirect_to posts_path, :notice => "Post Saved"
    else
        render "new"
    end
end

这是帖子表

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.string :venmo_name
      t.float :price
      t.datetime :end_datetime
      t.integer :areacode
      t.string :email

      t.timestamps null: false
    end
  end
end

1 个答案:

答案 0 :(得分:1)

  

未定义的方法`email ='

它说没有定义Post属性的email的setter方法。您的posts表很可能只是错过了该列,否则AR会为您生成它。

相关问题