我可以创建一个游戏,在那个游戏中我可以创建规则,我有一个“完成”规则的按钮,通过将规则布尔值:已完成列更改为 false,将其推送到“规则已完成”视图,我有一个通过将 Rule boolean :completed 列更改回 true 来“更新”规则的按钮。
所以,我的下一个任务是在新的 Score 控制器中创建一个 create 方法:
- 通过相同的“已完成”button_to 操作,
- 查找该 button_to 的规则并将该规则 ID 插入到新表 Score 中的“rule_id”列中。
- 它还将完成时间(用户点击按钮的时间)发布到分数表的“完成时间”列,
我尝试的是简单地复制规则控制器中 create 方法的大部分细节,即:
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
我的新分数数据库是:
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'