2010-06-14 42 views
0

我早些時候詢問了this question,並且獲得了我需要的部分路線。將一個模型的創建動作放在另一個模型的展示頁面

我有一個簡單的應用程序,有三個表。用戶,機器人和收件人。機器人屬於用戶和收件人屬於機器人。

在機器人顯示頁面上,我希望能夠在機器人顯示頁面內爲該機器人創建收件人。

我已經在機器人展示頁面下面的代碼會列出目前收件人:

<table> 

<% @robot.recipients.each do |recipient| %> 
    <tr> 
    <td><b><%=h recipient.chat_screen_name %></b> via <%=h recipient.protocol_name</td> 

    <td><%= link_to 'Edit', edit_recipient_path(recipient) %>&nbsp;</td> 
    <td><%= link_to 'Delete', recipient, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 
</table> 

,現在我有這樣的機器人表演的看法:

<% form_for(@recipient) do |f| %> 
Enter the screen name<br> 
<%= f.text_field :chat_screen_name %> 
<p> 
    <%= f.submit 'Update' %> 
</p> 
<% end %> 

和機器人控制器具有這添加到它也:

@recipient = Recipient.new 
    @recipients = Recipient.all 

我現在看到我想看到的領域,但它看起來像你h收件人仍然沒有與用戶所在的顯示頁面的機器人關聯。而且,當我擁有這段代碼時,創建時的重定向將轉到收件人索引頁面,而不是機器人顯示頁面。

我有這樣的代碼在受惠人士票價控制在創建行動:

我真的很感激任何額外的幫助。我知道我接近解決方案,但不是那裏.... :)

謝謝。

回答

1
@recipient = Recipient.new(params[:recipient]) 

這將根據提交的表單創建一個新的收件人,但表單中沒有指定要使用的機器人。因爲沒有params [:recipient] [:robot_id],所以不能有@ recipient.robot。

嘗試嵌套資源並從url params訪問robot id,或嘗試傳遞一個隱藏的字段,其中包含robot id並從提交的表單中訪問它。

關鍵點在這裏:params [:recipient]只包含從表單傳來的內容,並且你沒有在任何地方傳遞機器人id。

+0

Jarrod,謝謝你的解釋。我不知道如何獲得與其關聯的ID,並最終以隱藏字段結束。 – Andrew 2010-06-15 04:48:14

0

使用嵌套的路線,然後在創建:(這是一個稍微不同的方法,對方的回答)

@robot = Robot.find(params[:bug_id]) 
@recipient = Robot.recipients.new(params[:recipient]) 

這將自動創建@recipientrobot_id設置正確。

相關問題