2011-07-17 220 views
2

在我的應用程序中,用戶有很多對話,而對話有很多消息。我想創建一個新的對話:我必須指定用戶(讀者)和(第一個)消息。我嘗試了以下,但失敗了。Rails:在嵌套模型表單中使用accept_nested_attributes_for

模型

class Conversation < ActiveRecord::Base 

    has_many :conversation_users 
    has_many :users, :through => :conversation_users 

    has_many :messages 

    accepts_nested_attributes_for :users 
    accepts_nested_attributes_for :messages 

end 

class Message < ActiveRecord::Base 

    belongs_to :conversation 
    belongs_to :user 

end 

class User < ActiveRecord::Base 

    has_many :conversation_users 
    has_many :conversations, :through => :conversation_users 

end 

控制器

def new 
    @conversation = Conversation.new 
    2.times do 
    users = @conversation.users.build 
    end 
    messages = @conversation.messages.build 
end 

def create 
    @conversation = Conversation.new(params[:conversation]) 

    if @conversation.save 
    redirect_to username_conversations_path(current_username) 
    else 
    redirect_to new_username_conversation_path(current_username) 
    end 
end 

查看

<% form_for([current_user, @conversation]) do |f| %> 

    <% f.fields_for :users do |builder| %> 

    <%= builder.text_field :id %> 

    <% end %> 

    <% f.fields_for :messages do |builder| %> 

    <%= builder.text_area :content %> 

    <% end %> 

    <%= f.submit "Submit" %> 

<% end %> 

CURRENT_USER和current_username是定義輔助方法如下:

def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
end 

def current_username 
    @current_username ||= current_user.username if current_user 
end 

這是軌道服務器的響應:conversations_controller的

Started POST "https://stackoverflow.com/users/8/conversations" for 127.0.0.1 at Sun Jul 17 23:58:27 +0200 2011 
    Processing by ConversationsController#create as HTML 
    Parameters: {"commit"=>"Submit", "authenticity_token"=>"z6kL+NmVspgCKMr9whcw+a85mA59j3jssS9QeTiEbxc=", "utf8"=>"✓", "conversation"=>{"users_attributes"=>{"0"=>{"id"=>"9"}, "1"=>{"id"=>"10"}}, "messages_attributes"=>{"0"=>{"content"=>"freee"}}}, "user_id"=>"8"} 
    User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "conversation_users" ON "users".id = "conversation_users".user_id WHERE "users"."id" IN (9, 10) AND (("conversation_users".conversation_id = NULL)) 

ActiveRecord::RecordNotFound (Couldn't find User with ID=9 for Conversation with ID=): 
    app/controllers/conversations_controller.rb:26:in `new' 
    app/controllers/conversations_controller.rb:26:in `create' 

線26:@conversation = Conversation.new(params[:conversation])

我怎樣才能得到這個工作?

謝謝。

+0

你是什麼意思「失敗」?任何錯誤? – fl00r

+0

我添加了錯誤響應...這是一個記錄未找到錯誤 – alste

+0

我看不到你的迴應中的任何錯誤 – fl00r

回答

6
class Conversation < ActiveRecord::Base 
    has_many :conversation_users 
    has_many :users, :through => :conversation_users 
    has_many :messages 
    # NEXT LINE IS CHANGED! 
    accepts_nested_attributes_for :conversation_users 
    accepts_nested_attributes_for :messages 
end 

控制器

def new 
    @conversation = Conversation.new 
    2.times{ users = @conversation.conversation_users.build } 
    messages = @conversation.messages.build 
end 

,並形成

<%= form_for([current_user, @conversation]) do |f| %> 
    <%= f.fields_for :conversation_users do |builder| %>  
    <%= builder.text_field :user_id %> 
    <%= builder.hidden_field :conversation_id %> 
    <% end %> 
    ... 
<% end %> 

就是這樣。

+0

正確的,我創建一個新的'conversation_user'對象,而不是'用戶'對象!精彩:)感謝您的幫助 – alste

1

替換:

   <% f.fields_for :users do |builder| %> 

有了:

   <%= f.fields_for :users do |builder| %> 

同樣缺少 '=' 低一點。

+0

的權利 - 表單顯示正常...錯誤仍然 – alste

1
<% form_for([current_user, @conversation]) do |f| %> 

應該

<%= form_for([current_user, @conversation]) do |f| %> 

而且

<% f.fields_for :messages do |builder| %> 
# and 
<% f.fields_for :users do |builder| %> 

應該

<%= f.fields_for :messages do |builder| %> 
# and 
<%= f.fields_for :users do |builder| %> 
+0

+1 4秒前我的:) – apneadiving

+0

@apneadiving actualy反之亦然:) – fl00r

+0

是的,但錯誤仍然 - 我添加了更多的信息在錯誤 – alste

相關問題