So I’m building a blog application using Ruby on Rails, and when it
comes to implementing comments I follow the example laid down in the
blog example screencast. I’ve run into the same problem as several
others; that I can’t work out how I’m supposed to set parameters in my
comment creation method that aren’t given to it by the form view. So my
comment controller has a method like this:
@post = Post.find(params[:id])
@comment = @post.comments.create(params[:comment])
@comment.datetime = Time.now # Doesn’t work!
@comment.user_id = @user.id # Doesn’t work!
flash[:note] = ‘Comment was successfully created.’
redirect_to :action => ‘list’
render :action => ‘new’
The post ID is established correctly, my relationships are set up as
they should be in my models, and the :comment params are being passed
successfully, but the two lines after I create my @comment instance
variable don’t seem to have any effect. I’m getting database errors
saying there are NULL values where there shouldn’t be.
The only solutions I’ve found are to pass the values I want as hidden
fields in my form (a security risk), or add them using :before_create
hooks. Am I missing something or can what I want not be done through