Ruby on Rails 4 - Button_to没有按预期工作

时间:2015-09-03 18:29:02

标签: ruby-on-rails ruby button-to

我有一个用户可以接受或拒绝的挑战表,我无法让它正常工作。当用户点击接受按钮时,他应该收到通知,表示接受了挑战(不工作 - 始终不接受)并且网站应该刷新 - 有效

我使用了"丑陋" button_to的方法,因为我需要class和remote true属性。我知道 - "衰退"按钮不是uptodate因为我现在使用了接受按钮。

<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

我也总是进入错误循环,即使:接受是真的

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

0 个答案:

没有答案