2017-10-05 21 views
-1

今日、何か試していましたが、このエラーが発生しました。私は足場を介してビューを作成し、それは関連するモデルです。コントローラーで少し編集を加えましたが、これを把握することができず、このエラーが発生しています。Rails 5.1 ActionController :: UrlGenerationError in ...一致するルートがありません

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ビュー。

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

これは 'index'、小文字の' i'でなければなりません。慣例では、変数名とメソッド名には大文字を使用しないでください。 – tadman

関連する問題