2017-02-13 59 views
0

我試圖通過link_to更新game_started屬性。我也嘗試過使用form_for通過hidden_​​field沒有運氣。Rails - 通過鏈接傳遞參數的問題link_to

我也不斷收到以下錯誤

引發ArgumentError在GamesController#更新

在分配屬性,你必須通過一個哈希作爲參數。

使用Rails 5和Ruby 2.4

任何解釋,將不勝感激!

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 

回答

0

將其更改爲

if @game.update_attributes (game_started: params[:game_started]) 
    redirect_to @game 
end 
+0

太謝謝你了!已經解決了! – Sal

0

show.html.erb應更改爲

<%= link_to "Start The Game", game_path(@game, :game => {:game_started => true}), :method => :put %> 

控制器應該

if @game.update_attributes (game_started: params['game']['game_started']) 
    redirect_to @game 
end 
0

錯誤是告訴你,你傳遞了錯誤的參數傳遞到update_attributes方法調用。它期望像{game_started: params['game_started']}這樣的散列,而您只是將其值設爲params['game_started']。當你給它一個值時,它不會知道模型中的哪個字段要更新。所以,你的代碼更改爲:

```

if @game.update_attributes(game_started: params[:game_started]) 
    redirect_to @game 
end 

```