Rails dynamic increasing button

125 Views Asked by At

I have to do an increasing value button "bg" for p1 entry in table game but it is not working... It save the game with p1 = 0 but the link_to has no effect..

Many thanks for any help!!! (And sorry for my bad English...)

controller

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

views

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 %>

error message:

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

There are 1 best solutions below

1
Baldrick On

The :method option of link_to is for the HTTP verb (:get, :post, :put or :delete)

What you want to do is below (using the :put method, preferred verb for updates)

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

and in routes.rb:

put 'add_three_points' => 'games#add_three_points', :as => :add_three_points