在游戏中,我有一个10x10的网格,有100个正方形,我试图为一个正方形做一个按钮,当点击时将正方形的:user_id更改为current_user id。
游戏/ show.html.erb
<% @squares.each_slice(10) do |slice| %>
<% slice.each do |s| %>
<%= div_for s, class: 'sq' do %>
<%= s.boardposition %>
<%= button_to buy_path(s), remote: true, method: :put %>
<% end %>
<% end %>
<% end %>
games_controller.rb
def show
require 'enumerator'
@user = current_user
@game = Game.find(params[:id])
@squares = Square.where(game_id:@game.id).all
end
squares_controller.rb
def buy
@square = Square.find(square_params)
respond_to do |format|
format.js
if @square.update_attributes(user_id: current_user.id)
format.html {redirect_to squares_path}
else
format.html {redirect_to :back}
end
end
end
private
def square_params
params.require(:square).permit(:xvalue, :yvalue, :user_id, :game_id, :boardposition)
end
Game.rb
has_many :squares
has_and_belongs_to_many :users
validates :roomname, :roomtype, :teamone, :teamtwo, :gamedate, :squareprice,
:maxsquares, :q1pay, :q2pay, :q3pay, :q4pay, presence: true
accepts_nested_attributes_for :squares
Square.rb
belongs_to :user
belongs_to :game
User.rb
has_and_belongs_to_many :games
has_many :squares
accepts_nested_attributes_for :squares
路由
resources :games, :users, :squares
get '/games/:id/join/' => 'games#join', as: :join
get '/games/:id/buy/' => 'squares#buy', as: :buy
match '/games/:id/', to: 'squares#buy', via: [:post]
当我点击购买广场时我得到的错误是“SquareStroller中的ActionController :: ParameterMissing#buy”未找到Param:square。
我已经尝试了一段时间来解决这个问题,我相信我的路线现在是正确的,但我无法通过该参数。任何帮助将不胜感激。
答案 0 :(得分:0)
你没有传递方形物体
def square_params
**params.require(:square)**.permit(:xvalue, :yvalue, :user_id, :game_id, :boardposition)
end
以某种方式通过表单提供方形对象,或删除强参数。