0

I have a model "Post" which belongs_to "Group." The url is like this .../groups/26/posts/1 everything was working fine until I needed to add a user name to each post so I added a new foreign key to the Posts table for "User" and made the Post model belongs_to :user, and in the User model added has_many :posts.

My issue is that I don't know how to create a new post so that I can get both the group_id and user_id added to the posts table.

This works to populate the user_id foreign key of the Posts table. I need to call group in here somehow but can't make it work.

def new
  @posts = current_user.posts.build  
end

def create
  @post = current_user.posts.build(post_params)    

  if @post.save
    flash[:success] = "Post created successfully!"
    redirect_to group_url(@post.group)
  else
    render 'new'
  end
end   

This works to populate the group_id foreign key of the Posts table, I would need to call @user or current_user in here somehow but not sure how to do it.

# GET /groups/:group_id/posts/new
def new
  @posts = @group.posts.new 
end

def create
  @post = @group.posts.new(post_params)   

  if @post.save
    flash[:success] = "Post created successfully!"
    redirect_to group_url(@post.group)
  else
    render 'new'
  end
end   

2 Answers 2

1

You can add the user manually to both the new and create actions, like this:

# GET /groups/:group_id/posts/new
def new
  @post = @group.posts.new(user_id: current_user.id)
end

def create
  @post = @group.posts.new(post_params.merge(user_id: current_user.id))   

  if @post.save
    flash[:success] = "Post created successfully!"
    redirect_to group_url(@post.group)
  else
    render 'new'
  end
end   
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it by setting the @group/current_user when you do the build/new of the @post.

here is an example of how to add the group:

def new
  @posts = current_user.posts.build(group: @group)
end

def create
  @post = current_user.posts.build(post_params)  
  @post.group = @group  

  if @post.save
    flash[:success] = "Post created successfully!"
    redirect_to group_url(@post.group)
  else
    render 'new'
  end
end 

EDIT:

@moveson posted the other way, of adding the current_user to the @post created with from the @group (he posted while i wrote my answer :D )

EDIT 2:

Actually, i would go with the other way. (setting the current_user like the other answer)

BUT! do you really need to set the current user on the new method?

You can show the current_user info on the html page anyway, and then set it only in the create method! (when you actually going to save it to the DB....)

8 Comments

that's true, I probably only need it in the create action. thank you for your answer it helped me understand both ways of accomplishing this. much appreciated.
It is probably not necessary to add the user_id in the new action, but it can't hurt, and it may come in handy once you get to the form view.
@moveson if he uses devise - he can use current_user anyway in the views! :D but that way he can use @post.user if he needs it! @scott You are welcome
Yep, he certainly could. But I think it's more obvious and straightforward to just add the user when you make the object.
@moveson - yes and no. the new action doesn't creates the object in the db, it just send it to the view so you could use that data. When the user submits the form for creation, that data is sent back to the create method which is AGAIN creating an object, but that time saves it to the db. the user_id will be sent back to the create method from the form -> which is DANGEROUS! because users can edit (even hidden) form fields, so that data can be manipulated. @Scott should (and MUST in my opinion) use the current_user for safety on the server side.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.