Reputation: 739
I haven't coded in Rails for a while so struggling a bit.
When a user created a post via a form I made, how can I have the users user name be automatically saved with the post.
I use Facebook Omniauth for authentication.
When I for example search\index all the posts the name is on that post.
What will the code for the show page be?
Here is my index page code:
Name:
?????
From:
<%= post.from %>
To:
<%= post.to %>
Date:
<%= post.date %>
The Form:
<div class="field">
<%= f.label :name %>
<%= ? %>
</div>
<div class="field">
<%= f.label :from %>
<%= f.text_field :from, :class => 'address-picker-input' %>
</div>
<div class="field">
<%= f.label :to %>
<%= f.text_field :to, :class => 'address-picker-input' %>
</div>
<div class="field">
<%= f.label :date %>
<%= f.text_field :date, :class=> "field shorter" %>
</div>
The controller:
def create
@post = current_user.posts.build(params[:post])
Upvotes: 0
Views: 98
Reputation: 8331
In your controller you are passing posts from your current user:
def create
@post = current_user.posts.build(params[:post])
end
Just add a line and pass the user info:
@user_info = current_user.user_info
# => included data: name, nickname, first_name, last_name
Check the docs for more data you can work with.
Finally, you could easily implement the wished data in your view:
<div class="username">
<%= @user.name %>
</div>
Upvotes: 1