嵌套的意見,我有以下兩種模式訪問路徑,以在軌
class User < ActiveRecord::Base
has_many :attachments
end
class Attachment < ActiveRecord::Base
belongs_to :user
end
耙路線如下:
我想重定向到附件的index
一旦新用戶被創建。
目前,這就是我想: users_controller.rb
def create
@user = User.new(user_params)
@user.save
redirect_to user_attachments_path(@user)
end
index.html.erb
爲attachments
<% @user.attachments.each do |attachment| %>
<tr>
<td><%= attachment.name %></td>
<td><%= attachment.format %></td>
<td><%= link_to 'Download', download_user_attachment_path(attachment), :method => :get %></td>
<td><%= link_to 'Delete', user_attachments_path(attachment),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
我得到以下後,我進入了新用戶的詳細信息:
什麼我做錯了嗎? 如何訪問正確的路徑來訪問嵌套的屬性/視圖?
你遷移的記錄? – Elyasin
@Elyasin我沒有將'user_id'添加到'attachments'表中。補充說明已經解決了這個路障 – Sinstein