2017-10-05 103 views
-1

所以,我今天嘗試了一些東西,並且遇到了這個錯誤。我通過腳手架和相關模型創建了視圖。我在控制器中做了一些微小的修改,但我無法弄清楚,並且不斷收到這個錯誤。Rails 5.1 ActionController :: UrlGenerationError在...沒有路由匹配

ActionController::UrlGenerationError in E#index 
No route matches {:action=>"show", :b_id=>#<C id: 1, 
b_id: 1, ...>, :controller=>"d", :c_id=># 
<D id: 1, ...}, missing required keys: [:id]` 

我有幾個模型關聯,我只在索引視圖中遇到這個問題。我能夠創建對象,我只是無法在索引中顯示它們。

模型

User.rb 
class User < ApplicationRecord 
    belongs_to :company 
    has_many :bs 
    has_many :cs, through: :bs 
    has_many :ds, through: :cs 
end 

B.rb 
class B < ApplicationRecord 
    belongs_to :user 
    has_many :companies, through: :users, as: :company_users 
    has_many :cs, dependent: :destroy 
    has_many :ds, through: :cs 
end 

C.rb 
class C < ApplicationRecord 
    belongs_to :bs 
    has_many :ds 
    has_many :companies, through: :bs, source: :company_users 
end 

D.rb 
class D < ApplicationRecord 
    belongs_to :ds 
    has_many :companies, through: :cs, source: :company_users 
end 

控制器

class DController < ApplicationController 
    before_action :set_d, only: [:show, :edit, :update, :destroy] 

    # GET /appointments 
    # GET /appointments.json 
    def index 
    @d = b_c.ds 
    end 

    # GET /d/1 
    # GET /d/1.json 
    def show 
    end 

    # GET /d/new 
    def new 
    @d = c.ds.new 
    end 

    # GET /d/1/edit 
    def edit 
    end 

    # POST /d 
    # POST /d.json 
    def create 
    @d = c.ds.new(d_params) 
    respond_to do |format| 
     if @d.save 
     format.html { redirect_to [b, c, @d], notice: 'D was successfully created.' } 
     format.json { render :show, status: :created, location: @d } 
     else 
     format.html { render :new } 
     format.json { render json: @d.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /d/1 
    # PATCH/PUT /d/1.json 
    def update 
    respond_to do |format| 
     if @d.update(d_params) 
     format.html { redirect_to [c, @d], notice: 'D was successfully updated.' } 
     format.json { render :show, status: :ok, location: @D } 
     else 
     format.html { render :edit } 
     format.json { render json: @d.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /d/1 
    # DELETE /d/1.json 
    def destroy 
    @d.destroy 
    respond_to do |format| 
     format.html { redirect_to ds_url, notice: 'D was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_appointment 
     @d = D.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def appointment_params 
     params.require(:d).permit(:name, :phone, :start_time, :end_time, :c_id) 
    end 

    def c 
     @c ||= C.find(params[:c_id]) 
    end 

    def b 
     @b ||= B.find(params[:b_id]) 
    end 
end 

最後索引視圖。

index.html.erb 
... 
<% @d.each do |d| %> 
     <tr> 
     <td><%= d.name %></td> 
     <td><%= d.phone %></td> 
     <td><%= d.start_time %></td> 
     <td><%= d.end_time %></td> 
     <td><%= d.c %></td> 
     <td><%= link_to 'Show', b_c_d_path(@b_c, d) %></td> 
     <td><%= link_to 'Edit', edit_b_c_d_path(@b_c, d) %></td> 
     <td><%= link_to 'Destroy', b_c_d_path(@b_c, d), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
... 
+0

您已經在這裏簡化了您的變量名稱以避免錯誤。 'redirect_to [b,c,@d]'應該做什麼? – tadman

回答

0

答:我看到了我的錯誤在哪裏。我看着耙路,看到我不得不在路徑上添加.id到我的參數。例如。 @ b.id,@ c.id。然後在控制器上我像這樣定義它們。

def index 
    @b = b 
    @c = C 
    @d = b.find(params[:b_id]).c.find(params[:c_id]).d.all 
end 
+0

這應該是'索引',小寫'我'。通過約定,變量和方法名不應該有任何大寫字母。 – tadman

相關問題