id 、 name 、 e-mail などの User テーブルがあります...
現在ログインしているユーザー ID を別のテーブル Post に渡し、各投稿に対応するユーザー ID が含まれるようにします。
コントローラー側
**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
ビュー パーツ
**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%>
投稿テーブルには、userid、title、body、tag が含まれます
ユーザーが正常にログインしたときに、ユーザーIDの値をセッションに保存しました........
Post テーブルに id フィールドを追加する方法を教えてください.....