2014-09-05 35 views
0

我是Rails的新手,並且遇到了一個問題,我無法包裹頭部。has_many關聯的'new'和'edit'表單的錯誤

我用a:belongs_to /:has_many關係創建了2個模型。

class Inning < ActiveRecord::Base 
    belongs_to :match 
end 

class Match < ActiveRecord::Base 
    has_many :innings 
end 

我能夠得到父(匹配)模型正確工作,我也能夠給孩子(局)模型成功鏈接。 我的問題是,我收到一個錯誤,我無法理解或解決每當我嘗試打開新建或編輯窗體。

undefined method `inning_path' for #<#<Class:0x5751888>:0x54d3c08> 

的路由設置如下:

resources :matches do 
    resources :innings 
    end 

任何人都可以提供關於如何解決這個有什麼建議?

謝謝。

UPDATE:

表單代碼:

<%= form_for [@match, @inning] do |f| %> 
    <%= f.error_notification %> 
    <%= f.input(:inning_no) %> 
    <%= f.input(:batting_team) %> 
    <%= f.input(:bowling_team) %> 
    <%= f.input(:score) %> 
    <%= f.button(:submit) %> 
<% end %> 

耙路線:

  Prefix Verb URI Pattern         Controller#Action 
    match_innings GET /matches/:match_id/innings(.:format)   innings#index 
        POST /matches/:match_id/innings(.:format)   innings#create 
new_match_inning GET /matches/:match_id/innings/new(.:format)  innings#new 
edit_match_inning GET /matches/:match_id/innings/:id/edit(.:format) innings#edit 
    match_inning GET /matches/:match_id/innings/:id(.:format)  innings#show 
        PATCH /matches/:match_id/innings/:id(.:format)  innings#update 
        PUT /matches/:match_id/innings/:id(.:format)  innings#update 
        DELETE /matches/:match_id/innings/:id(.:format)  innings#destroy 
      matches GET /matches(.:format)       matches#index 
        POST /matches(.:format)       matches#create 
     new_match GET /matches/new(.:format)      matches#new 
     edit_match GET /matches/:id/edit(.:format)     matches#edit 
      match GET /matches/:id(.:format)      matches#show 
        PATCH /matches/:id(.:format)      matches#update 
        PUT /matches/:id(.:format)      matches#update 
        DELETE /matches/:id(.:format)      matches#destroy 

innings_controller.rb:

class InningsController < ApplicationController 


    before_action :set_match, only: [:index, :new, :create] 
    before_action :set_inning, only: [:show, :edit, :update, :destroy] 


    def index 
    #@innings = Inning.find(params[:match_id]) 
    @innings = @match.innings 
    end 

    def show 
    end 

    def new 
    @inning = @match.innings.new 
    end 
    def create 
    @inning = @match.innings.new(inning_params) 

    respond_to do |format| 
     if @inning.save 
     format.html { redirect_to @inning, notice: 'Inning was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @inning } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @inning.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def edit 
    end 

    def update 
    respond_to do |format| 
     if @inning.update(inning_params) 
     format.html { redirect_to @inning, notice: 'Inning was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render :edit } 
     format.json { render json: @inning.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @inning.destroy 
    respond_to do |format| 
     format.html { redirect_to match_innings_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_inning 
     @inning = Inning.find_by_id(params[:id]) 
    end 
    def set_match 
     if params[:match_id] 
     @match = Match.find_by_id(params[:match_id]) 
     end 
    end 
    def inning_params 
     params.require(:inning).permit(:inning_no, :batting_team, :bowling_team, :score, :match_id) 
    end 

end 
+0

你'set_match'應該在你的控制器所有的方法運行,刪除'only'東西 – Aguardientico 2014-09-05 03:24:42

+0

謝謝。 ..這工作完美。 – 2014-09-05 05:56:25

回答

1

將是巨大的查看你的「看法」,但我猜問題是你正在嘗試它:

form_for @inning代替form_for [@match, @inning]

而且在局控制器,你應該是這樣的:

before_action :set_match 

protected 
def set_match 
    @match = Match.find(params[:match_id]) 
end 
+0

也請把你的innings_controller.rb代碼,我要更新我的答案,試圖把我認爲在你的控制器中缺少的代碼 – Aguardientico 2014-09-05 03:08:38

1

既然你嵌套你局下資源相匹配,你的名字路線也將與比賽嵌套。

新局,命名路線將

new_match_inning_path 

你的路由將會像

GET  /matches/:match_id/innings(.:format) innings#index match_innings 
POST /matches/:match_id/innings(.:format) innings#create match_innings 
GET  /matches/:match_id/innings/new(.:format) innings#new new_match_inning 

and etc 

你可以閱讀文檔嵌套資源here

+0

我已經使用了基於嵌套路由的鏈接的正確路徑。不管怎麼說,還是要謝謝你。 – 2014-09-05 03:02:11