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 :
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 :
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):
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.
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?
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
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.