我有一张用户可以接受或拒绝的挑战表,但我无法让它正常工作。当用户单击接受按钮时,他应该收到一个通知,表明挑战已被接受(不起作用 - 它总是不被接受)并且应该刷新站点 -有效
我对 button_to 使用了“丑陋”的方式,因为我需要 class 和 remote true 属性。而且我知道 - “拒绝”按钮不是最新的,因为我现在使用接受按钮。
<tbody>
<% @challenges.each do |ch| %>
<tr>
<td><%= Game.find_by_id(Duel.find_by_id(ch.duel_id).game_id).name %></td>
<td><%= Duel.find_by_id(ch.duel_id).euro %></td>
<td><%= link_to "#{User.find_by_id(ch.challengedplayer).name}", user_path(User.find_by_id(ch.challengedplayer)) %></td>
<td><%= button_to 'Accept', { controller: :challenges, action: "reaction", ch_accepted: true, challenge: ch, challenge_id: ch.id }, method: :patch, data: { confirm: 'Do you really want to challenge this player?' }, class: 'button challenge', remote: true %></td>
<td><%= button_to 'Decline', { controller: :challenges, action: "reaction", accepted: false}, method: :patch, data: { confirm: 'Are you sure?' }, class: 'button challenge', remote: true%></td>
</tr>
<% end %>
</tbody>
这是控制器 - 我有一个巨大的问题,我似乎无法使用challenge_params。
def reaction
if :accepted == "true"
@challenge = Challenge.find(params[:challenge_id])
@challenge.open = false
@challenge.accepted = true
@challenge.save
flash[:notice] = "You successfully challenged #{User.find_by_id(@challenge.challengedplayer).name} for #{Duel.find_by_id(@challenge.duel_id).euro }€"
render :js => "window.location = 'challenges'"
else
flash[:notice] = "Error"
render :js => "window.location = 'challenges'"
end
end
private
def challenge_params
params.require(:challenge).permit(:accepted, :open, :duel_id, :challenge_id)
end
end
我也总是进入错误循环,即使 :accepted 是真的
Started PATCH "/reaction?ch_accepted=true&challenge=1&challenge_id=1" for ::1 at 2015-09-03 20:25:33 +0200
Processing by ChallengesController#reaction as JS
Parameters: {"authenticity_token"=>"rDT/OucYRavkRBXUGVpgtZsFG9eKAwRKNo+hSsc+VIJW1CfPBBqwlt7baRHrjz/g8n2dJX1ypjWVrk1Vs/Mgeg==", "ch_accepted"=>"true", "challenge"=>"1", "challenge_id"=>"1"}
这是路线文件
Rails.application.routes.draw do
resources :duels
patch 'reaction' => 'challenges#reaction'
resources :challenges
root to: 'duels#index'
devise_for :users
#, controllers: {registrations: "users/registrations"}
resources :users
end