Ruby on Rails
Monday, December 1, 2014
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 <anndrew78@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ae822215-0f1c-4efe-bcd9-70bc4bf97f87%40googlegroups.com.--sure.posts_controller.rb...privatedef post_paramsparams.require(:post).permit(:title, :body)endclass CreatePosts < ActiveRecord::Migrationdef changecreate_table :posts do |t|t.string :titlet.text :bodyt.integer :user_idt.timestampsendendendcomments_controller.rb...privatedef comment_paramsparams.require(:comment).permit(:body)endclass CreateComments < ActiveRecord::Migrationdef changecreate_table :comments do |t|t.integer :bodyt.integer :post_idt.integer :user_idt.timestampsendendendthank 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 <annd...@gmail.com> wrote:--Hi all!
I have 3 models: user, post, comment
User has_many :posts, :comments Post belongs_to :user, has_many :comments Comments 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' end end
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' end end
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 rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
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.
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAFKVRj9DMYFKst3240jXkRWUUGfjL5hVfx6DHPeQRAAmJJDYBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment