1
在遊戲中,我有一個10×10的網格與100個正方形,我試圖做一個正方形的按鈕,當點擊更改方塊的user_id到current_user ID。Rails嵌套窗體更新與Ajax錯誤
遊戲/ 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]
當我點擊購買廣場時出現的錯誤是「ActionController :: ParameterMissing in SquaresController#buy」Param not found:square。
我已經嘗試了一段時間來弄清楚這一點,我相信我的路線現在是正確的,但我無法通過參數。任何幫助將不勝感激。
有沒有什麼辦法可以傳遞'<%= button_to buy_path(s),remote:true,method :: put%>'中的方形對象?否則,我不確定如何爲's'對象創建表單,因爲它已經從方塊數組嵌套 – user3185958