I would recommend a different solution. You should not rely on an form to
capture the user_id, unless the user is actually selectable from a
dropdown. Yes, you could put the user_id in a hidden form field; however,
that leaves it open to be changed by the user.
Rather, I would do it this way --
PostController.rb
def new
@post = Post.new
end
def create
@post = current_user.posts.create post_params
end
private
def post_params
params.require(:post).permit(:title, :body)
end
CommentsController.rb
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.new
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new comment_params
@comment.user_id = current_user.id
if @comment.save
redirect_to @post
else
render action: new
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
I hope this is helpful
On Wednesday, December 3, 2014 8:17:52 AM UTC-6, Psycho Shine wrote:
>
> Thank you so much, brilliant solution)
>
> понедельник, 1 декабря 2014 г., 3:19:22 UTC-8 пользователь Vivek Sampara
> написал:
>>
>> def comment_params
>> params(:comment).merge!(:user_id => params[:user_id])
>> params.require(:comment).permit(:body, :user_id)end
>>
>>
>>
>> On Mon, Dec 1, 2014 at 4:29 PM, Psycho Shine <[email protected]> wrote:
>>
>>> sure.
>>>
>>>
>>> posts_controller.rb
>>>
>>> ...
>>> private
>>> def post_params
>>> params.require(:post).permit(:title, :body)
>>> end
>>>
>>> class CreatePosts < ActiveRecord::Migration
>>> def change
>>> create_table :posts do |t|
>>> t.string :title
>>> t.text :body
>>> t.integer :user_id
>>> t.timestamps
>>> end
>>> end
>>> end
>>>
>>>
>>> comments_controller.rb
>>> ...
>>> private
>>> def comment_params
>>> params.require(:comment).permit(:body)
>>> end
>>>
>>>
>>> class CreateComments < ActiveRecord::Migration
>>> def change
>>> create_table :comments do |t|
>>> t.integer :body
>>> t.integer :post_id
>>> t.integer :user_id
>>> t.timestamps
>>> end
>>> end
>>> end
>>>
>>>
>>> thank you
>>>
>>> воскресенье, 30 ноября 2014 г., 11:47:19 UTC-8 пользователь Vivek
>>> Sampara написал:
>>>>
>>>> Could you show an example of what post_params contains ?
>>>>
>>>> On Sun, Nov 30, 2014 at 4:24 PM, Psycho Shine <[email protected]>
>>>> wrote:
>>>>
>>>>> 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
>>>>> <https://groups.google.com/d/msgid/rubyonrails-talk/8a1c4cf8-cf08-4dad-b934-d0f27ce21684%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> --
>>> 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/ae822215-0f1c-4efe-bcd9-70bc4bf97f87%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/rubyonrails-talk/ae822215-0f1c-4efe-bcd9-70bc4bf97f87%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
--
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/d4423d37-a571-4843-8580-91a11c0e8e00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.