Trouble saving current user

I have an application where users can log in and create records. I am
trying to get Rails to save the id/name of the user who created the
record. Somehow, the user id isn’t getting stored, and I can’t tell
why.

I’m rather new to Rails, and I’m cobbling together different tips from
different tutorials (and advice from friendly people here), and my guess
is that I’ve just mushed too much stuff together and don’t know how to
integrate it all gracefully. Or, just as likely, there’s some glaring
typo in my code.

In any case, here is the relevant part of the controller:

---------------------words_controller.rb-------------------
class WordsController < ApplicationController
before_filter :login_required, :only => [ :new, :create, :edit,
:update, :delete ]

def new
@word = Word.new
end
def create
@modword = (Modword.find_by_modspell(params[:modword][:modspell])
|| Modword.create(params[:modword]))
@word = @current_user.words.build(params[:word])
@word = Word.new(params[:word].merge(:modword_id => @modword.id))
if @word.save
redirect_to(:action => :index)
else
render :action => ‘new’
end
end

end

Any idea what’s going on here? If you need more information, I’ll be
happy to supply it!

Thanks so much!

Nevermind. I changed to the following code, and now it works:

def create
@modword = (Modword.find_by_modspell(params[:modword][:modspell])
|| Modword.create(params[:modword]))
@word = Word.new(params[:word].merge(:modword_id => @modword.id))
@word.user_id = @current_user.id
if @word.save
redirect_to(:action => :index)
else
render :action => ‘new’
end
end

You could also do:

@word = Word.new(params[:word].merge(:modword_id =>  

@modword.id, :user_id => @current_user.id))

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs