Building a Browser Game: write a method for counting the "points" - using this method in links

Hello all,

i want to create a method that i can use to update/count the points of a village/user in my browser game. Every time the user clicks on a link, the method should be used. The links should look something like that :

<%= link_to('Update', village_path, :method => :update_points) %>

right or not?

The question is now: Where do i write this method? I did a test in the village_controller:

def update_points

    @village = Village.find(params[:id])

    @village.points = 120     @village.save

end

can this work?

cause when i click on the link i just get :

No route matches "/villages/12"

When i open the /villages/12 without the method it works all fine. My routes.rb :

Citywar::Application.routes.draw do

  devise_for :users   resources :users, :only => [:index, :show]   resources :villages   root :to => 'pages#home'

end

So how can i solve this?

Best regards Genji

Genji wrote in post #1016197:

Hello all,

i want to create a method that i can use to update/count the points of a village/user in my browser game. Every time the user clicks on a link, the method should be used. The links should look something like that :

<%= link_to('Update', village_path, :method => :update_points) %>

right or not?

Not. In rails, a method within a controller is called 'an action'--not 'a method'. In the http protocol, which is the syntax everyone agrees to use over the internet, the 'method' is usually GET or POST, which specifies how data is to be sent over the internet. If you don't know the difference, do some research. Therefore, when you write:

:method => :update_points

that is wrong because it does not specify 'get' or 'post' for the method. By default, links use the method GET. If you want to change some data on the server, then you should specify POST.

The action that the link_to goes to can be tricky to figure out. rails will automatically work out what action it should execute based on the path you specify and the method(= get or post). See the chart in section 2.2 here:

Note that the '/photos' path will cause rails to execute a different action depending on the method. If the automatic routing doesn't work for you, you can specify the controller and the action in link_to (instead of a path):

<%= link_to "Click me",             {:controller => 'users', :action => 'get_info'} %>

Sometimes you don't even have to specify a path and rails will still figure out where to go. See the examples in the link_to docs:

You have another problem, too. You used the 'named route': village_path. village_path requires an argument, which may look like this: village_path(@village). Compare that to the named route: villages_path.

So when you see :method it The question is now: Where do i write this method? I did a test in the village_controller:

def update_points

    @village = Village.find(params[:id])

    @village.points = 120     @village.save

end

can this work?

Why don't you want to use the conventional update action? Also, what page do you want your rails app to send back in response to the update? Do you want your app to remain on the same page? Remember that when you call an action, rails automatically sends back a page to the browser that matches the action name, e.g. rails converts update_points.html.erb. into update_points.html and sends it back to the browser. Then the browser loads the page.

Well i just didn't think about the option. Thank you a lot , i got it now. Can i ask you another Question?

I have different Buildings and it should be possible to upgrade these buildings.

So my link to count the points looks like this now:

<%= link_to 'Update', village_path(@village), :method => :put %>

It uses the conventional update action now, but writing the code for an Building_upgrade action in this update action doesn't sound good to me. So can i write an action in the village controller and use it like this?

<%= link_to 'Update', village_path(@village), :method => :put , :action => building_upgrade %>

or how can i handle this?

When do you want to update a Building?

The buildings got a "lvl" the lvl is the same like their id in their table( a little useless maybe ^^'). So I want to write an upgrade action. This should be like this: On the Village show page, the buildings of the Village are listed. You can click on a link or button to "upgrade" one Building. The code checks if u have enough resources, if its not the "max - lvl" , the tests pass: Village.building_id should get + 1 . To get the building with the higher lvl. Village.save

I don't touch the building table, it's all in the village.

For example The code does this: Village.pit_id = Village.pit_id + 1

( <Pit id: 1, lvl: 1, points: 5, rate: 12, created_at: "2011-08-11 13:51:11", updated_at: "2011-08-11 13:51:11"> to   <Pit id: 2, lvl: 2, points: 12, rate: 18, created_at: "2011-08-11 13:51:11", updated_at: "2011-08-11 13:51:11">

i hope you can figure out what i mean...

Genji wrote in post #1016242:

So can i write an action in the village controller and use it like this?

<%= link_to 'Update', village_path(@village), :method => :put , :action => building_upgrade %>

No. This:

  village_path(@village)

produces the path for the link. As I showed in my earlier post, you can specify the path yourself like this:

<%= link_to "Click me",             {:controller => 'users', :action => 'get_info'} %>

Note that in both cases, the path is the second argument to link_to.

So can i write an action in the village controller and use it

You can write any action you want in your controller, then as long as you provide a route for it in your route.db file, you can use the proper path in your link to call the action.

i hope you can figure out what i mean...

Not really. As far as I can tell, Building is irrelevant.