I have a User table with id , name , e-mail etc…
i want to pass the current logged user id into another table Post , so that each post contain corresponding user id.
controller side
**app/controller/post_controller.rb**
class PostController < ApplicationController
def index
@post=Post.new
@posts=Post.all
end
def create
@post=Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to :controller=>"post" ,:action=>"index" }
format.json { render json: @post, status: :created, location: @post }
#redirect_to :controller=>"post" ,:action=>"index"
else
format.html { redirect_to :controller=>"home" ,:action=>"index" }
format.json { render json: @post, status: :created, location: @post }
end
end
end
view part
**app/views/post/questions.html.erb**
<%=form_for :post do |f| %>
<table>
<tr>
<td>
Title:<%=f.text_field :title %>
</td>
</tr>
<tr>
<td>
<h3>Question</h3>
</td>
</tr>
<tr>
<td>
<%=f.text_area :body %>
</td>
</tr>
<tr>
<td>
Tag:<%=f.text_field :tag %>
</td>
</tr>
<tr>
<td>
<h3><%= f.submit "Post Your Question" %></h3>
</td>
</tr>
</table>
<%end%>
Post table contain userid , title ,body ,tag
i have stored the user id value in session when user successfully logs in……..
please tell me how to add id field into Post table…..
In create method instead of this
@post=Post.new(params[:post])change it to thisand you will be good to go