Mateusz Urban
2013-May-23 11:56 UTC
How to do it the rest way ? Question from fledgeling Ruby programmer
Hi! Reading "Rails For .NET Developers" i started to consider the example of building the flight system with cancellations. So, I''ve generated flights scaffold and passengers scaffold and I''d like that we could cancel the flight and then, each of the passengers would be informed about that fact. So, firstly i''ve written a non-rest code and simply added "cancel_flight" method to my FlightsController and generated required routes in routes.rb. And it looks like this:> def cancel_flight > > flight = Flight.find(params[:flight]) > > > > flight.cancel_flight > > > > redirect_to flights_path > > > > end > > (where Flight model has a method cancel_flight, which of course knows whatto do) It''s all working, but then I tried to do it the rest way. So, i considered added new resource called FlightCancellation, but I don''t want it to represent any database model. So it looks like this> class FlightCancellation > > attr_reader :flight > > > > def initialize(flight) > > @flight = flight > > end > > > > end > >And then in my FlightCancellations controller, I''ve something like this>class FlightCancellationsController < ApplicationController def create cancelation = params[:flight_cancelation] flight = cancelation.flight flight.cancel_flight redirect_to :back, :notice => ''Flight was cancelled'' end end In routes.rb i have> resources :flight_cancelations, :only => :createAnd now, i''m confused about how to embbed it in my view ? I was trying to do it like this> <% @flights.each do |flight| %> > > <tr> > > <td><%= flight.from %></td> > > <td><%= flight.to %></td> > > <td><%= link_to ''Show'', flight %></td> > > <td><%= link_to ''Edit'', edit_flight_path(flight) %></td> > > <td><%= link_to ''Destroy'', flight, method: :delete, data: { confirm: >> ''Are you sure?'' } %></td> > > <td> > > <%= form_for FlightCancelation.new(flight) do |f| %> > > <%= f.submit_tag ''Cancel that flight'' %> > > <% end %> > > </td> > > </tr> > > <% end %> > >Could anybody tell me how can I achive this, and if my way of thinking is right ? Thanks in advance, Mateusz Urban -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9f27bdeb-1388-40ba-9f42-bf1e4c4c88ec%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-May-23 19:27 UTC
Re: How to do it the rest way ? Question from fledgeling Ruby programmer
On 23 May 2013 12:56, Mateusz Urban <mateuszurban2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > Reading "Rails For .NET Developers" i started to consider the example of > building the flight system with cancellations. So, I''ve generated flights > scaffold and passengers scaffold and I''d like that we could cancel the > flight and then, each of the passengers would be informed about that fact. > So, firstly i''ve written a non-rest code and simply added "cancel_flight" > method to my FlightsController and generated required routes in routes.rb. > And it looks like this: >>> >>> def cancel_flight >>> >>> flight = Flight.find(params[:flight]) >>> >>> >>> >>> flight.cancel_flight >>> >>> >>> >>> redirect_to flights_path >>> >>> >>> >>> end > > (where Flight model has a method cancel_flight, which of course knows what > to do)There is no need for a separate controller, one way would be to handle it within the flights controller Edit action, using a url parameter or the Submit button name to indicate that cancel is required rather than edit. However, REST should not be treated as a religion to which all requirements must bow. I would say there is nothing wrong with what you have done (provided that you have used a POST to do it). Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuWuNFDdrWHujGYaGkfw4%3DUKtew61Fhz9U%2B67OEdWHz%3Dw%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2013-May-23 20:30 UTC
Re: How to do it the rest way Question from fledgeling Ruby programmer
Colin Law wrote in post #1109974:> There is no need for a separate controller, one way would be to handle > it within the flights controller Edit action, using a url parameter or > the Submit button name to indicate that cancel is required rather than > edit. > However, REST should not be treated as a religion to which all > requirements must bow. I would say there is nothing wrong with what > you have done (provided that you have used a POST to do it).Don''t you mean PATCH/PUT rather than POST? POST would indicate you are creating a new flight on the collection of flights, rather than changing the state of an existing flight. Sure POST would work (and technically PATH/PUT is simulated using a POST), but using PATCH/PUT would be more conventional in a RESTful application. I agree it seems to be overkill in this case to create a separate controller. I see nothing wrong with adding a "cancel" method to the flight controller. Having additional methods in a controller isn''t un-RESTful IMHO. You have a URL (http://example.com/flights/1/cancel) that represents a change (PATCH/PUT) to an existing resource. What''s un-RESTful about that? http://guides.rubyonrails.org/routing.html#adding-more-restful-actions If I were designing a system like this I would most likely implement my "Flight" model as a finite-state machine and use additional controller actions (along with the 7 default ones) to transition the flights through their various states. https://en.wikipedia.org/wiki/Finite-state_machine -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0e3f1abdf810fc4512863ba205d5b8dd%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-May-23 20:55 UTC
Re: Re: How to do it the rest way Question from fledgeling Ruby programmer
On 23 May 2013 21:30, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1109974: >> There is no need for a separate controller, one way would be to handle >> it within the flights controller Edit action, using a url parameter or >> the Submit button name to indicate that cancel is required rather than >> edit. >> However, REST should not be treated as a religion to which all >> requirements must bow. I would say there is nothing wrong with what >> you have done (provided that you have used a POST to do it). > > Don''t you mean PATCH/PUT rather than POST? POST would indicate you are > creating a new flight on the collection of flights, rather than changing > the state of an existing flight. Sure POST would work (and technically > PATH/PUT is simulated using a POST), but using PATCH/PUT would be more > conventional in a RESTful application.Yes, I was speaking loosely (which is a cardinal sin) and using POST to mean one of the set of non-GET verbs. I am showing my age, going back to the days when there were only the two options. Colin> > I agree it seems to be overkill in this case to create a separate > controller. I see nothing wrong with adding a "cancel" method to the > flight controller. > > Having additional methods in a controller isn''t un-RESTful IMHO. You > have a URL (http://example.com/flights/1/cancel) that represents a > change (PATCH/PUT) to an existing resource. What''s un-RESTful about > that? > > http://guides.rubyonrails.org/routing.html#adding-more-restful-actions > > If I were designing a system like this I would most likely implement my > "Flight" model as a finite-state machine and use additional controller > actions (along with the 7 default ones) to transition the flights > through their various states. > > https://en.wikipedia.org/wiki/Finite-state_machine > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0e3f1abdf810fc4512863ba205d5b8dd%40ruby-forum.com?hl=en-US. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtJxXaj8DkcYEBJoEMZfn4mjk_S86OvP7KOgaPmjQir5g%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2013-May-23 22:11 UTC
Re: Re: How to do it the rest way Question from fledgeling Ruby programmer
Colin Law wrote in post #1109983:> On 23 May 2013 21:30, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> creating a new flight on the collection of flights, rather than changing >> the state of an existing flight. Sure POST would work (and technically >> PATH/PUT is simulated using a POST), but using PATCH/PUT would be more >> conventional in a RESTful application. > > Yes, I was speaking loosely (which is a cardinal sin) and using POST > to mean one of the set of non-GET verbs. I am showing my age, going > back to the days when there were only the two options. > > ColinWell technically there are still only two options from inside a web browser. Still can''t fathom why that hasn''t been fixed in modern browsers. :) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/213441ca3e5b4c5f3bfc2b6fec53e6dc%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.