在我relationships_controller我有以下幾點:我是否正確處理這個Rails窗體?
class RelationshipsController < ApplicationController
def new
@user_id = User.find_by_id(params[:user_id])
@relationship = Relationship.new
end
def create
@relationship = Relationship.new(params[:relationship])
@relationship.rel_id = User.find_by_id(params[:user_id])
@relationship.user_id = current_user
if @relationship.save
redirect_to root_url, :notice => "Signed Up!"
else
render "new"
end
end
end
,在我的觀點,我有:
<section id="main">
<%= form_for [@user_id, @relationship] do |f| %>
<div class="field">
<%= f.label :type %>
<%= select_tag(:type, options_for_select([['Friend', 0], ['Family', 1],['Spouse', 2]])) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
</section>
我有幾個問題:
這是正確的方法處理rel_id和user_id?對我來說這似乎有點笨重。
我不能得到:類型保存到數據庫,但其他一切。我發現在我的服務器日誌如下:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"z7R4tWSSVHZmFXfh8HocfyuegZ2rwuXXeTLKbR+cLfs=", "type"=>"0", "commit"=>"Create Relationship", "user_id"=>"7"}
這似乎很奇怪我,因爲它要保存類型。
3.如果我在<%= form_for [@user_id, @relationship] do |f| %>
行中使用@user_id或@current用戶,這有什麼關係嗎?爲什麼?
'@ user'是人'@ current_user'好友。他們應該始終是不同的用戶。我還沒有編碼檢查。 –
我在Nesting Comments上觀看了Railscast#139,這裏的建議讓我覺得嵌套這是合適的。你有什麼理由不相信嗎? –
在這種情況下嵌套的資源很好。我只是有點不確定你想要用戶@user與current_user相比。 –