我收到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
答案 0 :(得分:0)
路由(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
答案 1 :(得分:0)
不确定这是否会导致错误,但在模型文件中
has_one :fixtures, :class_name => 'Fixture', :foreign_key => :fixture_id
应该只是:
has_one :fixture
总是一个好的提示:has_one = singular,has_many =复数