【问题标题】:No route matches {:action=>“show”, :controller=>“”}没有路线匹配 {:action=>“show”, :controller=>“”}
【发布时间】:2013-01-02 00:46:00
【问题描述】:

我收到No route matches {:action=>“show”, :controller=>“predictions”} 错误,但不明白为什么。我猜这可能与我的模型关系有关,我可能需要添加一条路线。

预测模型

class Prediction < ActiveRecord::Base
  attr_accessible :fixture_id, :predicted_result
  has_one :fixtures, :class_name => 'Fixture', :foreign_key => :fixture_id
  belongs_to :fixture
  belongs_to :user
end

预测控制器

class PredictionsController < ApplicationController
  # GET /predictions
  # GET /predictions.json
  def index
    @predictions = Prediction.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @predictions }
    end
  end

  # GET /predictions/1
  # GET /predictions/1.json
  def show
    @prediction = Prediction.find(params[:id])
    @fixture = Fixture.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @prediction }
    end
  end

  # GET /predictions/new
  # GET /predictions/new.json
  def new
    @prediction = Prediction.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @prediction }
    end
  end

  # GET /predictions/1/edit
  def edit
    @prediction = Prediction.find(params[:id])
  end

  # POST /predictions
  # POST /predictions.json
  def create
    @prediction = Prediction.new(params[:prediction])

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

  # PUT /predictions/1
  # PUT /predictions/1.json
  def update
    @prediction = Prediction.find(params[:id])

    respond_to do |format|
      if @prediction.update_attributes(params[:prediction])
        format.html { redirect_to @prediction, notice: 'Prediction was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @prediction.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /predictions/1
  # DELETE /predictions/1.json
  def destroy
    @prediction = Prediction.find(params[:id])
    @prediction.destroy

    respond_to do |format|
      format.html { redirect_to predictions_url }
      format.json { head :no_content }
    end
  end
end

显示视图

<p id="notice"><%= notice %></p>

<p>
  <b>Fixture:</b>
  <%= @prediction.fixture_id %>
</p>

<p>
  <b>Predicted result:</b>
  <%= @prediction.predicted_result %>
</p>

<%= link_to 'Edit', edit_prediction_path(@prediction) %> |
<%= link_to 'Back', predictions_path %>


>rake routes
      users_new GET    /users/new(.:format)            users#new
    predictions GET    /predictions(.:format)          predictions#index
                POST   /predictions(.:format)          predictions#create
 new_prediction GET    /predictions/new(.:format)      predictions#new
edit_prediction GET    /predictions/:id/edit(.:format) predictions#edit
     prediction GET    /predictions/:id(.:format)      predictions#show
                PUT    /predictions/:id(.:format)      predictions#update
                DELETE /predictions/:id(.:format)      predictions#destroy
       fixtures GET    /fixtures(.:format)             fixtures#index
                POST   /fixtures(.:format)             fixtures#create
    new_fixture GET    /fixtures/new(.:format)         fixtures#new
   edit_fixture GET    /fixtures/:id/edit(.:format)    fixtures#edit
        fixture GET    /fixtures/:id(.:format)         fixtures#show
                PUT    /fixtures/:id(.:format)         fixtures#update
                DELETE /fixtures/:id(.:format)         fixtures#destroy
          teams GET    /teams(.:format)                teams#index
                POST   /teams(.:format)                teams#create
       new_team GET    /teams/new(.:format)            teams#new
      edit_team GET    /teams/:id/edit(.:format)       teams#edit
           team GET    /teams/:id(.:format)            teams#show
                PUT    /teams/:id(.:format)            teams#update
                DELETE /teams/:id(.:format)            teams#destroy
          users GET    /users(.:format)                users#index
                POST   /users(.:format)                users#create
       new_user GET    /users/new(.:format)            users#new
      edit_user GET    /users/:id/edit(.:format)       users#edit
           user GET    /users/:id(.:format)            users#show
                PUT    /users/:id(.:format)            users#update
                DELETE /users/:id(.:format)            users#destroy
           root        /                               fixtures#index

【问题讨论】:

  • 你能添加你的 routes.rb 吗?
  • 重要的缺失细节:OP 正在访问/predictions 并得到关于显示而不是索引页面的错误。 /predictions/1 确实工作。
  • 你能发布你的错误跟踪吗

标签: ruby-on-rails routes action runtime-error show


【解决方案1】:

不确定这是否会导致错误,但在您的模型文件中

has_one :fixtures, :class_name => 'Fixture', :foreign_key => :fixture_id

应该只是:

has_one :fixture

总是一个很好的提示:has_one = 单数,has_many = 复数

【讨论】:

    【解决方案2】:

    路由 (config/routes.rb) 应该包括:

    resources :users
    resources :predictions
    resources :fixtures
    

    在命令行(从应用程序的根目录)输入rake routes 以查看您拥有哪些路由。

    最初,改变:

    def show
        @prediction = Prediction.find(params[:id])
        @fixture = Fixture.find(params[:id])  
        # Note: for fixture, the id would be different, prob fixture_id
    

    def show
        @prediction = Prediction.find(params[:id])
    

    即卸下夹具。您始终可以使用关系在视图中显示它,即@prediction.fixture_id@prediction.fixture.name

    【讨论】:

    • 我已经包含了所有这些路线,这就是我感到困惑的原因
    • 请输入rake routes,你会得到什么输出?谢谢
    • 我从预测控制器中删除了@fixture,但我仍然看到路由错误。不幸的是,我可以粘贴我看到的内容,因为我的字符有限,但是如果有帮助,我会在运行 rake 路线时看到列出的所有模型?
    • 哪个 LINE 出现错误。请提供确切错误的堆栈跟踪。
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2012-07-17
    • 2011-12-25
    • 2022-12-07
    相关资源
    最近更新 更多