在视频游戏评论网站上进行练习。
创建新的视频游戏对象时,我必须为它们分配平台(xbox,playstation等)。
我的表单如下,Game模型有很多平台,而Platforms模型属于游戏
目前我使用收藏选择。如何将其转换为复选框,并能够使用复选框选择多个平台并保存?
非常感谢。
服务器日志
Started PATCH "/games/6" for ::1 at 2015-07-08 05:07:59 -0400
Processing by GamesController#update as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"n9+Tnko0702pGPDbC3/rzdqGx92yR
ZZymKw3zd+dLwjl+zVqlrEa1yby1/xeXJVeXr+sht8XgXOD0ytCL+dnMw==", "game"=>{"title"=>
"Test", "release_date(1i)"=>"2015", "release_date(2i)"=>"7", "release_date(3i)"=
>"8", "genre_id"=>["1", ""], "platform_id"=>["1", ""]}, "commit"=>"Update Game",
"id"=>"6"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDE
R BY "users"."id" ASC LIMIT 1 [["id", 1]]
Game Load (1.0ms) SELECT "games".* FROM "games" WHERE "games"."id" = ? LIMIT
1 [["id", 6]]
Unpermitted parameters: genre_id, platform_id
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/games/6
Completed 302 Found in 10ms (ActiveRecord: 1.0ms)
Started GET "/games/6" for ::1 at 2015-07-08 05:07:59 -0400
Processing by GamesController#show as HTML
Parameters: {"id"=>"6"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDE
R BY "users"."id" ASC LIMIT 1 [["id", 1]]
Game Load (0.0ms) SELECT "games".* FROM "games" WHERE "games"."id" = ? LIMIT
1 [["id", 6]]
(0.0ms) SELECT COUNT("previews"."vote") FROM "previews" WHERE "previews"."ga
me_id" = ? [["game_id", 6]]
CACHE (0.0ms) SELECT COUNT("previews"."vote") FROM "previews" WHERE "previews
"."game_id" = ? [["game_id", 6]]
(0.0ms) SELECT COUNT("reviews"."vote") FROM "reviews" WHERE "reviews"."game_
id" = ? [["game_id", 6]]
CACHE (0.0ms) SELECT COUNT("reviews"."vote") FROM "reviews" WHERE "reviews"."
game_id" = ? [["game_id", 6]]
News Load (1.0ms) SELECT "news".* FROM "news" WHERE "news"."game_id" = ? OR
DER BY "news"."id" DESC LIMIT 5 [["game_id", 6]]
Rendered games/_review.html.erb (2.0ms)
Review Load (1.0ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."game_i
d" = ? [["game_id", 6]]
(0.0ms) SELECT COUNT(*) FROM "reviews" WHERE "reviews"."game_id" = ? [["gam
e_id", 6]]
Review Load (0.0ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."game_
id" = ? ORDER BY "reviews"."id" DESC LIMIT 5 [["game_id", 6]]
Rendered games/show.html.erb within layouts/application (16.0ms)
Completed 200 OK in 162ms (Views: 157.1ms | ActiveRecord: 2.0ms)
服务器日志2使用Emu的建议< - 仍未保存,但未允许的参数错误消失。
Started PATCH "/games/6" for ::1 at 2015-07-08 05:56:55 -0400
Processing by GamesController#update as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"lzku1Wa89d6a2i2yArfyiyQwfhLcW
iigah0Pv6XpUAjtHYghujkARBUwCpVXlIwYoAkVSbEIP6FxYhMwVZMYMw==", "game"=>{"title"=>
"Test", "release_date(1i)"=>"2015", "release_date(2i)"=>"7", "release_date(3i)"=
>"8"}, "platform_ids"=>["4", "5"], "commit"=>"Update Game", "id"=>"6"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDE
R BY "users"."id" ASC LIMIT 1 [["id", 1]]
Game Load (0.0ms) SELECT "games".* FROM "games" WHERE "games"."id" = ? LIMIT
1 [["id", 6]]
(0.0ms) begin transaction
(0.0ms) commit transaction
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/games/6
Completed 302 Found in 9ms (ActiveRecord: 0.0ms)
Started GET "/games/6" for ::1 at 2015-07-08 05:56:55 -0400
Processing by GamesController#show as HTML
Parameters: {"id"=>"6"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDE
R BY "users"."id" ASC LIMIT 1 [["id", 1]]
Game Load (0.0ms) SELECT "games".* FROM "games" WHERE "games"."id" = ? LIMIT
1 [["id", 6]]
(0.0ms) SELECT COUNT("previews"."vote") FROM "previews" WHERE "previews"."ga
me_id" = ? [["game_id", 6]]
CACHE (0.0ms) SELECT COUNT("previews"."vote") FROM "previews" WHERE "previews
"."game_id" = ? [["game_id", 6]]
(0.0ms) SELECT COUNT("reviews"."vote") FROM "reviews" WHERE "reviews"."game_
id" = ? [["game_id", 6]]
CACHE (0.0ms) SELECT COUNT("reviews"."vote") FROM "reviews" WHERE "reviews"."
game_id" = ? [["game_id", 6]]
News Load (0.0ms) SELECT "news".* FROM "news" WHERE "news"."game_id" = ? OR
DER BY "news"."id" DESC LIMIT 5 [["game_id", 6]]
Rendered games/_review.html.erb (7.0ms)
Review Load (0.0ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."game_i
d" = ? [["game_id", 6]]
(0.0ms) SELECT COUNT(*) FROM "reviews" WHERE "reviews"."game_id" = ? [["gam
e_id", 6]]
Review Load (1.0ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."game_
id" = ? ORDER BY "reviews"."id" DESC LIMIT 5 [["game_id", 6]]
Rendered games/show.html.erb within layouts/application (26.0ms)
Completed 200 OK in 197ms (Views: 177.1ms | ActiveRecord: 3.0ms)
游戏桌
create_table "games", force: :cascade do |t|
t.string "title"
t.string "image"
t.date "release_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "platform_id"
t.integer "genre_id"
t.integer "review_id"
t.integer "steelbook_id"
end
_game.html.erb< - 如果我更改复选框以使用" genre_ids"和" platform_ids"我在游戏编辑控制器
中得到了无方法错误<%= form_for @game do |g| %>
<p><%= g.label :title %></p>
<p><%= g.text_field :title %></p>
<p><%= g.label :release_date %></p>
<p><%= g.date_select :release_date %></p>
<p><%= g.label :genre_id %></p>
<p><%= g.collection_check_boxes :genre_id, @genres, :id, :category %></p>
<p><%= g.label "Platform(s)" %></p>
<p><%= g.collection_check_boxes :platform_id, @platforms, :id, :system %></p>
<p><%= g.label :image %></p>
<p><%= g.file_field :image %></p>
<hr>
<p><%= g.submit %></p>
<% end %>
游戏控制器
class GamesController < ApplicationController
before_action :authenticate_user!
before_action :set_game, only: [:show, :edit, :update, :destroy]
def index
@games = Game.all
end
def show
@reviews = Review.where(game_id: @game.id)
@previews = Preview.where(game_id: @game.id)
@news = News.where(game_id: @game.id)
end
def create
@game = Game.new(game_params)
@game.save
redirect_to @game
end
def new
@game = Game.new
set_platforms
set_genres
end
def edit
set_platforms
set_genres
end
def update
@game.update(game_params)
redirect_to @game
end
def destroy
@game.destroy
redirect_to root_path
end
private
def game_params
params.require(:game).permit(:title, :image, :release_date, :genre_ids => [], :platform_ids => [])
end
def set_game
@game = Game.find(params[:id])
end
def set_platforms
@platforms = Platform.order(:system)
end
def set_genres
@genres = Genre.order(:category)
end
end
路由
Rails.application.routes.draw do
devise_for :users
root "games#index"
resources :games do
resources :news
resources :reviews, except: [:show]
resources :previews, except: [:show]
end
resources :platforms
resources :genres
end
平台模型
class Platform < ActiveRecord::Base
belongs_to :game
end
游戏模型
class Game < ActiveRecord::Base
has_many :platforms
has_many :preview, through: :users
has_many :reviews, through: :users
has_many :news
has_one :genre
end
答案 0 :(得分:0)
对于每个平台,使用以下循环创建一个新复选框:
<% @platforms.each do |platform| %>
<%= check_box_tag "platform_ids[]", platform.id %>
<%= platform.name %> // I assume that your platform has a name which can be used as a label
<% end %>
在控制器中,您可以使用params[:platform_ids]
只需使用以下代码更改控制器中的 game_params 方法:
def game_params
params.require(:game).permit(:title, :image, :release_date, :genre_id, :platform_ids => [])
end
或强> 您可以看到railscasts视频。
答案 1 :(得分:0)
您可以使用collection_check_boxes。
<p><%= g.collection_check_boxes :platform_ids, @platforms, :id, :system %></p>
并将game_params
更改为以下内容。
def game_params
params.require(:game).permit(:title, :image, :release_date, :genre_id, :platform_ids => [])
end
没有测试过它。我想你需要在代码中进行一些调整。