誰かが私を助けてくれるかどうか知りたいです。私は今RORを学んでいて、いくつかのトラブルに遭遇しました。scaffold を使用して other_users と post モデルを作成しました。other_users には多数の投稿があります。アイデアは、ユーザーがログインして投稿を作成すると、show アクションで、この投稿を作成したユーザーの名前が表示されるということです。誰かがこれで私に電話できるかどうか知りたい.
ポストコントローラー
def new
@post = Post.new(:other_user_id => @other_user.id)
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
投稿ビュー_フォーム:
<%= form_for(:post, :url => {:action => 'create', :other_user_id => @other_user.id}) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
何かが間違っていることは知っていますが、私はそれについてかなり新しいので、自分でそれを理解することはできません