访问rails中嵌套视图的路径

时间:2015-06-23 16:43:45

标签: ruby ruby-on-rails-4

我有以下两个模型

class User < ActiveRecord::Base
  has_many :attachments
end

class Attachment < ActiveRecord::Base
  belongs_to :user
end

佣金路线如下: enter image description here

我想在创建新用户后重定向到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 %>

输入新用户的详细信息后,我收到以下信息: enter image description here

我做错了什么? 如何访问正确的路径以访问嵌套的属性/视图?

1 个答案:

答案 0 :(得分:1)

问题与路线无关......

user_id表中缺少attachments

rails generate migration add_users_id_to_attachments user_id:integer

rake db:migrate