Hi all!
I have 3 models: user, post, comment
User has_many :posts, :commentsPost belongs_to :user, has_many
:commentsComments belongs_to :user, post
So for creating post i have action create
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
endend
Now if following the rules of relations between Post and Comment, for
creating a comment i have next action in CommentController:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
redirect_to post_path(@post)end
My question is: How rewrite the actions if i want create post and comment
from user?
if i try to create Comment from User, i create next action (don't know am i
right?)
def create
@user = User.find(params[:user_id])
@post = user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to post_path
else
flash[:errors] = "Post not created!"
render 'new'
endend
How create a comment from user, that will be in relations with post? i mean
comment must have user_id and post_id?
tnx 4 help
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/8a1c4cf8-cf08-4dad-b934-d0f27ce21684%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.