Re: [Rails] user_id being saved as NULL... but why?
This sounds like you don't have auto-increment turned on, on the
specific database table. Check that first?
-j
On May 8, 2010, at 9:15 AM, RubyonRails_newbie wrote:
> Hello there,
>
> I have a mini blog in which stores a few bits of info:
>
> - title
> - body
> - created on
> - user_id
>
> If you're logged in, you can enter a comment too.
> this stores:
>
> - body
> - blogpost_id
> - user_id
>
> however. For some reason, the user id when the blog is created is
> stored. It is not stored when a user adds a comment, and I wondered if
> anyone can spot what i ave missed?
>
> Actions from the Blogpost controller:
>
> def new
> @user = User.find(session[:user_id])
> #@blogpost = Blogpost.new
> @blogpost = Blogpost.create(:body => params[:body], :user_id =>
> session[:user_id])
>
> @cat = Category.find(:all)
> respond_to do |format|
> format.html # new.html.erb
> format.xml { render :xml => @blogpost }
> end
> end
>
> def create
> @user = User.find(session[:user_id])
> @blogpost = Blogpost.new(params[:blogpost])
> #@blogpost.category_id = (params[:category])
>
> respond_to do |format|
> if @blogpost.save
> flash[:notice] = 'Blogpost was successfully created.'
> format.html { redirect_to(@blogpost) }
> format.xml { render :xml => @blogpost, :status
> => :created, :location => @blogpost }
> else
> format.html { render :action => "new" }
> format.xml { render :xml => @blogpost.errors, :status
> => :unprocessable_entity }
> end
> end
> end
>
> And then the comment: (this is also in the same controller)
>
> def comment
> @user = User.find(session[:user_id])
> @blogpost = Blogpost.new
> @blogpost = Blogpost.create(:body => params[:body], :user_id =>
> session[:user_id])
>
>
> Blogpost.find(params[:id]).comments.create(params[:comment])
>
> flash[:notice] = "Added your comment"
>
> redirect_to :action => "show", :id => params[:id]
>
> end
>
> THe coment does get stored to the database, but the user_id remains
> NULL.
>
> Any ideas?
>
> thanks for reading.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-
> talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home