我可以创建一个游戏,在那个游戏中我可以创建规则,我有一个 通过更改规则布尔值“完成”规则的按钮 :已完成列为false,将其推送到“规则已完成” 通过更改规则,我有一个“更新”规则的按钮 boolean:已完成列回到true。
因此,我的下一个任务是在新的Score控制器中创建一个create方法:
我所尝试的只是简单地复制创建的大部分细节 Rule控制器中的方法,即:
def create
@rule = @game.rules.new(params[:rule])
if @rule.save
flash[:notice] = "You have added a Rule to your Game!"
redirect_to game_url(@game)
else
flash[:error] = "We couldn't add your Rule."
redirect_to game_url(@game)
end
end
我最近的努力是将rule_id发布到Score表,列“rule_id”,如下所示:
def create
@rule = Rule.find(params[:id])
@score = @rule.scores.new(params[:rule_id])
if @score.save
flash[:notice] = "You scored!"
redirect_to game_url(@game)
else
flash[:error] = "Wide right, try again."
redirect_to game_url(@game)
end
end
我的新分数db是:
class CreateScores < ActiveRecord::Migration
def change
create_table :scores do |t|
t.integer :rule_id
t.datetime :completed_time
t.timestamps
end
end
end
我建议的分数按钮操作是:
<%= button_to "Score!", score_path(@game.id,rule.id) %>
和路由设置为:
Tgom::Application.routes.draw do
resources :games do
resources :rules do
resources :scores do
end
end
end
match 'games/:game_id/rules/:id/complete' => 'rules#complete', :as => :complete_rule
match 'games/:game_id/rules/:rule_id/scores' => 'scores#create', :as => :score
match 'games/:game_id/rules/:id/uncomplete' => 'rules#uncomplete', :as => :uncomplete_rule
root :to => 'games#index'
此设置的当前错误是:
ActiveRecord::RecordNotFound in ScoresController#create
Couldn't find Rule without an ID
Rails.root: c:/Sites/tgom
app/controllers/scores_controller.rb:9:in `create'
答案 0 :(得分:0)
score_path
网址(根据您的路线)您的请求将设置:game_id
和:rule_id
参数。
特别是如果按
<%= button_to "Score!", score_path(@game.id,rule.id) %>
params[:game_id]
将@game.id
而params[:rule_id]
将为rule.id
但你的得分控制器创建方法是读取params[:id]
,这将是零,因此你的错误。