【问题标题】:Rails 5.1 ActionController::UrlGenerationError in... No route matchesRails 5.1 ActionController::UrlGenerationError in...没有路由匹配
【发布时间】:2017-10-05 21:57:21
【问题描述】:

所以,我今天尝试了一些东西,但遇到了这个错误。我通过脚手架及其关联模型创建了视图。我在控制器中做了一些细微的修改,但我无法弄清楚并不断收到此错误。

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 %>
...

【问题讨论】:

  • 您已将变量名称缩写到此处难以辨认的程度。 redirect_to [b, c, @d] 应该做什么?

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

回答:我看到了我的错误所在。我查看了 rake 路由,发现我必须将 .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

【讨论】:

  • 应该是index,小写i。按照约定,变量和方法名称不应包含任何大写字母。
猜你喜欢
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
相关资源
最近更新 更多