Genji
2011-Aug-11 15:00 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-11 16:58 UTC
Re: Building a Browser Game: write a method for counting the "points" - using this method in links
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: http://guides.rubyonrails.org/routing.html 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: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to 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.> > 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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Genji
2011-Aug-11 19:36 UTC
Re: Building a Browser Game: write a method for counting the "points" - using this method in links
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? On 11 Aug., 18:58, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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: > > http://guides.rubyonrails.org/routing.html > > 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: > > http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#... > > 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. > > > > > > > > > > > > > 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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-12 05:49 UTC
Re: Building a Browser Game: write a method for counting the "points" - using this method in links
When do you want to update a Building? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Genji
2011-Aug-12 10:50 UTC
Re: Building a Browser Game: write a method for counting the "points" - using this method in links
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... On 12 Aug., 07:49, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> When do you want to update a Building? > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-12 20:24 UTC
Re: Building a Browser Game: write a method for counting the "points" - using this method in links
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 itYou 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.