我有以下两个模型
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 %>
输入新用户的详细信息后,我收到以下信息:
我做错了什么? 如何访问正确的路径以访问嵌套的属性/视图?
答案 0 :(得分:1)
问题与路线无关......
user_id
表中缺少attachments
。
rails generate migration add_users_id_to_attachments user_id:integer
和
rake db:migrate