2012-12-05 17 views
5

私はゴールのために/config/routes.rbにのRuby on Railsのネストされたリソース未定義のメソッドパス

resources :goals do 
    resources :goal_entries 
    end 

モデルをネストされたリソースを定義した

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

'未定義のメソッド 'goal_entry_path''あなたのルートにgoal_entry_pathがありますか?もしあれば、編集方法を投稿できますか? – Magicmarkker

+0

あなたの 'edit'アクションは' @goal_entry = GoalEntry.find(params [:id]) 'を持っていますか? – MrYoshiji

+1

「レーキルート」がプリントアウトするものを投稿できますか? – Matt

答えて

8

あなたのform_forは[@goal, @goal_entry]からルートを生成していますが、更新アクションで@goalを定義していません。

更新方法に@goal = Goal.find(params[:goal_id])を追加してみてください。

+0

ありがとうDelameko! –

+0

あなたは私の問題を解決しました。 –

+0

私はまだ理解していません、なぜ検証メッセージがない場合、すべてがうまくいくのですか?そして、私が持っているメッセージはちょっと混乱します。それは、 'goal_entry_path'という定義されておらず、 'goal_path'ではないと言いますか?私は確かに何かを欠いている。 –

関連する問題