简体   繁体   English

在rails 3中使用“ create!”后如何保存user_id?

[英]how to save the user_id after using “create!” in rails 3?

I've recently updated my controller code to this for create 我最近将我的控制器代码更新为此

class MicropostsController < ApplicationController
  before_filter :signed_in_user

   def create
  @micropost = current_user.microposts.build(params[:micropost])


  if @micropost.review
      params[:micropost].delete :review
      @thread = Discussion.create!(params[:micropost])    
    else
      @micropost.save

    end
    redirect_to root_path
  end

When I use the above, it seems to work as the Discussion or the Micropost gets created. 当我使用上面的方法时,它似乎可以在创建“讨论”或“ Micropost”时使用。 However, I believe that using "create!" 但是,我相信使用“创建!” does not save the user_id. 不保存user_id。 When I view the Discussions, the user_id is nil. 当我查看讨论时,user_id为零。 How can I save the user_id with whoever made the post? 我如何与发帖的人一起保存user_id?

Tables in schema.db schema.db中的表

create_table "users", :force => true do |t|
    t.string    "name"
    t.string    "email"
    t.timestamp "created_at",                         :null => false
    t.timestamp "updated_at",                         :null => false
    t.string    "password_digest"
    t.string    "remember_token"
    t.boolean   "admin",           :default => false
  end




  create_table "discussions", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

The create! create! method basically runs a new and then a save! 方法基本上运行一个new ,然后save! throwing a validation exception if there is one. 如果有则抛出一个验证异常。

When you write this: 当您编写此代码时:

@micropost = current_user.microposts.build(params[:micropost])

It initializes (normally) a new micropost with the current_user id as if you had run: 它将(通常)使用current_user id初始化一个新的微博,就像您已经在运行一样:

@micropost = Micropost.new(params[:micropost].merge(user_id: current_user.id))

And @micropost.save saves it... Which in short does exactly the same as your create! 然后@micropost.save保存它...简而言之,它与您的create!完全相同create! method. 方法。

In conclusion I think you should delete this line: 总之,我认为您应该删除以下行:

@micropost = Micropost.create!(params[:micropost])

Change 更改

@thread = Discussion.create!(params[:micropost])   

To: 至:

@thread = current_user.discussions.create(params[:micropost])

This will get you the user_id 这将为您获取user_id

However... I think your controller is trying to "do too much". 但是...我认为您的控制器正在尝试“做太多”。 If this is the create action for microposts Why would it create a discussion and not a micropost ? 如果这是microposts的创建动作,为什么会创建discussion不是 micropost

Also if there are validation errors in that @micropost.save the user won't get any feedback, it'll just fail silently. 另外,如果在@micropost.save存在验证错误,则用户将不会获得任何反馈,它只会静默失败。 Same goes for the discussion create, you don't want them to get the generic http status 500 error page "Something went wrong" when the discussion fails validation when you use the create! 讨论创建同样如此,您不希望他们在使用create!时讨论未能通过验证时获得通用的http status 500错误页面“出了点问题” create! method. 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM