【问题标题】:Rails 3 nested resources - form partial only works for either new or edit, not bothRails 3 嵌套资源 - 部分形式仅适用于新建或编辑,而不适用于两者
【发布时间】:2013-03-29 10:30:06
【问题描述】:

我有嵌套资源,我正在尝试为每个单独的资源创建表单部分,以用于每个资源的新建和编辑操作。

routes.rb

resources :accounts, shallow: true,   :except => [ :destroy ] do
  resources :service,                 :except => [ :destroy ]
end

如果我在部分表单中使用以下内容,编辑表单会正确呈现并正确保存更新,但新表单无法呈现错误undefined method services_path

/app/views/services/_service_form.html.erb

<%= simple_form_for @service do |f| %>

如果我在表单部分中使用以下内容,新表单会正确呈现并正确保存对象,但编辑表单无法呈现错误undefined method account_service_path

/app/views/services/_service_form.html.erb

<%= simple_form_for [@account, @service] do |f| %>

我已尝试将url: service_path(@service) 添加到两个版本的表单块声明中,如图所示,但这并不能解决任何一个问题。

在让这个工作正常工作时,我肯定会缺少一些明显的东西,我确信过去我已经使用单个表单部分正确地工作了嵌套资源,我只是看不出这次有什么不同。

【问题讨论】:

  • 您是否在编辑操作中设置了@account 变量?我认为如果 @account 由例如 @account = @service.account 设置,它应该与 &lt;%= simple_form_for [@account, @service] do |f| %&gt; 一起使用。

标签: ruby-on-rails-3 forms routes


【解决方案1】:

将您的代码更改为以下 将 :service 更改为 :services

鉴于您的帐户模型 has_many :服务 和服务模型有 belongs_to : 帐号

resources :accounts, shallow: true,   :except => [ :destroy ] do
  resources :services,                :except => [ :destroy ]
end

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    相关资源
    最近更新 更多