Ruby on rails 在Ruby中的一个单独的表中为一个动作创建分数
我可以创造一个游戏,在这个游戏中我可以创造规则,我有一个 通过更改规则布尔值来“完成”规则的按钮 :completed列为false,将其推送到“Rules completed” 视图中,我有一个按钮,通过更改规则来“更新”规则 布尔值:已完成的列返回true 因此,我的下一个任务是在新的分数控制器中创建一个create方法:Ruby on rails 在Ruby中的一个单独的表中为一个动作创建分数,ruby-on-rails,ruby,scoring,Ruby On Rails,Ruby,Scoring,我可以创造一个游戏,在这个游戏中我可以创造规则,我有一个 通过更改规则布尔值来“完成”规则的按钮 :completed列为false,将其推送到“Rules completed” 视图中,我有一个按钮,通过更改规则来“更新”规则 布尔值:已完成的列返回true 因此,我的下一个任务是在新的分数控制器中创建一个create方法: 通过相同的“完成”按钮来执行操作 查找该按钮的规则并将该规则ID插入到新的 “规则id”列中的分数表 它还发布完成时间(用户点击 按钮)到分数表的“完成时间”列 我尝试的
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
我重复这一点的最新努力是将规则id发布到分数表的“规则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
我的新分数是:
class CreateScores < ActiveRecord::Migration
def change
create_table :scores do |t|
t.integer :rule_id
t.datetime :completed_time
t.timestamps
end
end
end
此设置的当前错误为:
ActiveRecord::RecordNotFound in ScoresController#create
找不到没有ID的规则
Rails.root:c:/Sites/tgom
app/controllers/scores\u controller.rb:9:in'create'
您通过得分路径
url(根据您的路线)提出的请求将设置:游戏id
和:规则id
参数。
特别是如果你按下按钮
<%= button_to "Score!", score_path(@game.id,rule.id) %>
params[:game\u id]
将是@game.id
,params[:rule\u id]
将是rule.id
但是您的分数控制器创建方法正在读取参数[: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'
<%= button_to "Score!", score_path(@game.id,rule.id) %>