2012-12-05 193 views
5

我在/config/routes.rb定義嵌套資源Ruby on Rails的嵌套資源未定義的方法路徑

resources :goals do 
    resources :goal_entries 
    end 

模型目標:

class Goal < ActiveRecord::Base 
    attr_accessible :code, :description, :from_date, :to_date 
    validates_uniqueness_of :code 
    validates_presence_of :code 
    has_many :goal_entries, :primary_key => "code", :foreign_key => "goal_code" 
    accepts_nested_attributes_for :goal_entries 
end 

和GoalEntry:

class GoalEntry < ActiveRecord::Base 
    attr_accessible :code, :goal_code, 
    :general_increase_percentage, :general_net_sales, 
    belongs_to :goal, :primary_key => "code", :foreign_key => "goal_code" 
    validates_presence_of :code 
    validates_presence_of :goal_code 
    validates_uniqueness_of :code , :scope => :goal_code 
    #validates_numericality_of :general_net_sales 

爲目標父級創建/編輯GoalEntry的視圖開始像這樣:

<%= form_for([@goal, @goal_entry]) do |f| %> 
<% if @goal_entry.errors.any? %> 
<div id="error_explanation"> 
    <h2><%= pluralize(@goal_entry.errors.count, "error") %> 
     prohibited this goal_entry from being saved: 
    </h2> 
    <ul> 
     <% @goal_entry.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
</div> 
<% end %> 
<div class="field"> 
    <%= f.hidden_field :goal_code, :required => true%> 
</div> 
<div class="field"> 
    <%= f.label :code %> 
    <%= f.number_field :code, :required => true %> 
</div> 
... 

在目標條目控制器更新方法:

def update 
    @goal_entry = GoalEntry.find(params[:id]) 

    respond_to do |format| 
     if @goal_entry.update_attributes(params[:goal_entry]) 
     format.html { redirect_to edit_goal_path(@goal_entry.goal), notice: 'Goal entry was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @goal_entry.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

輸入有效的目標項目工作正常。但是,如果有一個驗證錯誤消息我得到以下信息:

**NoMethodError in Goal_entries#update** 
ActionView::Template::Error (undefined method `goal_entry_path' for #<#<Class:0x007fce30a0c160>:0x00000004199570>): 
    1: <%= form_for([@goal, @goal_entry]) do |f| %> 
    2: <% if @goal_entry.errors.any? %> 
    3: <div id="error_explanation"> 
    4: <h2><%= pluralize(@goal_entry.errors.count, "error") %> 
    app/views/goal_entries/_form.html.erb:1:in `_app_views_goal_entries__form_html_erb__827873423371803667_70261777948540' 
    app/views/goal_entries/edit.html.erb:3:in `_app_views_goal_entries_edit_html_erb__779650500016633360_70261777920720' 
    app/controllers/goal_entries_controller.rb:77:in `block (2 levels) in update' 
    app/controllers/goal_entries_controller.rb:72:in `update' 
    etc... 

是不是有什麼毛病<% if @goal_entry.errors.any? %> 如果有人有一個解決方案,我會感激。謝謝

+2

'undefined method'goal_entry_path''你確定你的路線中有一個goal_entry_path嗎?如果有,你可以發佈你的編輯方法嗎? – Magicmarkker

+0

您的'編輯'行動是否具有以下'@ goal_entry = GoalEntry.find(params [:id])'? – MrYoshiji

+1

你可以發佈什麼'rake routes'打印出來嗎? – Matt

回答

8

您的form_for正在從[@goal, @goal_entry]生成其路線,但您尚未在更新操作中定義@goal。

嘗試將@goal = Goal.find(params[:goal_id])添加到您的更新方法中。

+0

謝謝Delameko! –

+0

你解決了我的問題。 –

+0

我還是不明白,爲什麼一切正常,當沒有驗證信息!我得到的消息有點混亂:它說未定義'goal_entry_path'而不是'goal_path'?我肯定錯過了一些東西。 –

相關問題