add user_id to new record

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

add user_id to new record

fugee ohu
How do i add user_id to a new record without placing it in a form as a hidden field? I tried adding in the controller's create action just before `if @post.save?` but that didn't cause new records created to have a value for user_id Maybe it was because I'm using a nested form and this record is a child record

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: add user_id to new record

Walter Lee Davis
In your Controller's #new and #create methods, scope the post to be owned by the current_user (or whatever other scheme you may be using for your logged-in-user management).

def new
  @post = current_user.posts.build
end

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

Now your relationship will take care of getting the correct user in there automatically, whether it's in-memory or in persistence.

NB: If this doesn't appear to work, then you haven't built your relationships correctly, and you need to fix that before proceeding.

Walter

> On Mar 2, 2020, at 12:59 AM, fugee ohu <[hidden email]> wrote:
>
> How do i add user_id to a new record without placing it in a form as a hidden field? I tried adding in the controller's create action just before `if @post.save?` but that didn't cause new records created to have a value for user_id Maybe it was because I'm using a nested form and this record is a child record
>
> --
> 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 [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/48BB2E5C-C543-4ACA-BA4F-CB474CB9256A%40wdstudio.com.
Reply | Threaded
Open this post in threaded view
|

Re: add user_id to new record

fugee ohu


On Monday, March 2, 2020 at 7:47:37 AM UTC-5, Walter Lee Davis wrote:
In your Controller's #new and #create methods, scope the post to be owned by the current_user (or whatever other scheme you may be using for your logged-in-user management).

def new
  @post = current_user.posts.build
end

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

Now your relationship will take care of getting the correct user in there automatically, whether it's in-memory or in persistence.

NB: If this doesn't appear to work, then you haven't built your relationships correctly, and you need to fix that before proceeding.

Walter

> On Mar 2, 2020, at 12:59 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="5c5FXX2XAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> How do i add user_id to a new record without placing it in a form as a hidden field? I tried adding in the controller's create action just before `if @post.save?` but that didn't cause new records created to have a value for user_id Maybe it was because I'm using a nested form and this record is a child record
>
> --
> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="5c5FXX2XAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/3903125c-1f66-4ed3-89dd-b73620d797f2%40googlegroups.com.


Ok thanks And what about passing some params to build?
  @post = current_user.posts.buil(artist_id: @artist.id, tour_id: @tour.id)

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2fe822d3-bcff-4b54-8f8e-f7893a9733fa%40googlegroups.com.