I'm trying to update the game_started attribute through a link_to. I've also tried using a form_for via hidden_field with no luck.
I also keep getting the below errors
ArgumentError in GamesController#update
When assigning attributes, you must pass a hash as an argument.
Using Rails 5 and Ruby 2.4
Any explanation would be greatly appreciated!
show.html.erb
<% if @game.game_started %>
# some code
<% else %>
<%= link_to "Start The Game", game_path(@game, :game_started => true), :method => :put %>
<% end %>
GamesController
def edit
end
def update
@game = Game.find(params[:id])
if @game.update_attributes (params[:game_started])
redirect_to @game
end
end
def game_params
params.require(:game).permit(:game_type, :deck_1, :deck_2, :user_1, :user_2, :game_started)
end
Change it to
if @game.update_attributes (game_started: params[:game_started])
redirect_to @game
end
In show.html.erb should change to
<%= link_to "Start The Game", game_path(@game, :game => {:game_started => true}), :method => :put %>
In Controller should be
if @game.update_attributes (game_started: params['game']['game_started'])
redirect_to @game
end
The error is telling you that you're passing the wrong arguments into the update_attributes
method call. It's expecting a hash like {game_started: params['game_started']}
, while you're just giving it the value of params['game_started']
. When you just give it a value, it won't know which field in the model to update. So change your code to:
```
if @game.update_attributes(game_started: params[:game_started])
redirect_to @game
end
```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.