【问题标题】:Why can't I edit a nested model now?为什么我现在不能编辑嵌套模型?
【发布时间】:2010-12-18 02:20:24
【问题描述】:

我收到以下错误:

undefined method `campaign_fax_path' for #<ActionView::Base:0xb6515200>

我的路线定义如下:

map.resources campaigns, :has_many => :faxes

错误发生在这里:

NoMethodError in Faxes#edit

Showing app/views/faxes/_form.html.erb where line #1 raised

我使用了一个非常基本的形式:

 <% form_for [@campaign, @fax] do |f| %>
  2   <%= f.error_messages %>
  3   <p>
  4     Campaign:
  5     <%= link_to @campaign.name, campaign_path(@campaign) %>
  6   </p>

当我做一个 rake routes | grep "campaign_fax*" 我得到以下信息:

         campaign_faxes GET    /campaigns/:campaign_id/faxes(.:format)               {:action=>"index", :controller=>"faxes"}
       new_campaign_fax GET    /campaigns/:campaign_id/faxes/new(.:format)           {:action=>"new", :controller=>"faxes"}

【问题讨论】:

  • 你在rake routes 的任何地方看到campaign_faxis 吗?是的,那是faxis,带有“i”。

标签: ruby-on-rails nested-routes


【解决方案1】:

我假设您使用的是 rails 2.3.X

在你的 routes.rb 中试试这个

map.resources campaigns do |campaigns|
  campaigns.resources :faxes
end

见:Nested Routes Section of the Rails Guides

【讨论】:

  • 我修复了它,但是通过阅读指南,无法回忆起它是什么。
【解决方案2】:

显示第 1 行出现的 app/views/faxes/_form.html.erb

也许部分有问题。你检查了吗?

map.resources campaigns, :has_many => :faxes **is that correct? I don't think so... maybe is:

map.resources campaign, :has_many => :faxes or

map.resources campaign, :has_many => :faxes

map.resources fax, :has_many => :campaigns

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多