开发者

Ruby on rails: Session/current_user, db-insert

I'm having 开发者_StackOverflow中文版problems with an insert to the database. First an explanation of my little Blog app.

The models: Users och Posts. http://pastie.org/2694864 A post have columns: title, body, user id

3 controllers: Session, Application (with current_user) and PostController: http://pastie.org/2695386

My loggin session seems to work but when a logged in user shoult write a post the database doesn't recognize any user_id. It's just set to nil. rails console:

=> #<Post id: 17, title: "hello", body: "hello world", created_at: "2011-10-14 14:54:25", updated_at: "2011-10-14 14:54:25", user_id: nil>

I guess it's in the post controller line 88 this should be fixed but I can't figure it out. I have also tried:

    @post = Post.new(params[:post], :user_id => session[:user_id])

But the user_id stills sets to nil!

This is my first app so I would be really greatful for detaild answears.

Tanx!


The problem is that you're passing Post.new two arguments (two hashes in this case), but it only takes one argument. Try this:

@post = Post.new(params[:post].merge!(:user_id => session[:user_id]))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜