Rails动态增加按钮

时间:2013-06-23 15:03:57

标签: ruby-on-rails link-to-remote

我必须为桌面游戏中的p1条目执行增加值按钮“bg”但它不起作用...它保存游戏时p1 = 0但是link_to没有效果..

非常感谢任何帮助! (抱歉我的英语不好......)

控制器

def create

@game = Game.new(params[:game])
@game.p1 = 0


respond_to do |format|
  if @game.save
    format.html { render action: "show" }
  end  
    end 
end

def show

@game = Game.find(params[:id])

respond_to do |format|
  format.js {render 'hnew'}
  end

end

def add_three_points
  @game = Game.find(params[:id])
  @game.update_attribute(:p1, @game.p1 + 3)
end

视图

show.html.erb

<%= render 'hnew' %>

hnew.js.erb

$('#ajax_hidden').html("<%= escape_javascript(render('hnew')) %>");

_hnew.html.erb

<%= link_to "bg", add_three_points_path(@game.id), {remote: true, method: :put}, 
    class: 'btn btn-small' %>
<%= @game.p1 %>

错误消息:

Started PUT "/add_three_points.14" for 127.0.0.1 at 2013-07-10 21:27:01 +0200
Processing by GamesController#add_three_points as 
Completed 404 Not Found in 1ms

ActiveRecord::RecordNotFound (Couldn't find Game without an ID):
app/controllers/games_controller.rb:86:in `add_three_points'

1 个答案:

答案 0 :(得分:0)

:method的{​​{1}}选项适用于HTTP动词(link_to:get:post:put

您要执行的操作如下(使用:delete方法,更新的首选动词)

:put

并在routes.rb中:

<%= link_to "bg", add_three_points_path(@game), {remote: true, method: :put}, class: 'btn btn-small' %>
相关问题