ruby - how to save the user_id after using "create!" in rails 3? -


i've updated controller code 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 use above, seems work discussion or micropost gets created. however, believe using "create!" not save user_id. when view discussions, user_id nil. how can save user_id whoever made post?

tables in schema.db

create_table "users", :force => true |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 |t|     t.string   "content"     t.integer  "user_id"     t.datetime "created_at", :null => false     t.datetime "updated_at", :null => false   end 

the create! method runs new , save! throwing validation exception if there one.

when write this:

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

it initializes (normally) new micropost current_user id if had run:

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

and @micropost.save saves it... in short same create! method.

in conclusion think should delete line:

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

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -