ruby on rails - How to I pass a object ID saved in one view to another view? -
i using rails build app follows. want create load multiple stops. have class load "has_many :stops". once load structure created save load , take user new stop view. how take load id created in last click , pass on stop? here have in new stop.
<%= label_tag :load_id %><br /> <%= number_field_tag :load_id %>
create nested resource, load_id in url: /loads/:load_id/stops/new
.
in load model:
class load < activerecord::base has_many :stops end
routes:
resources :loads resources :stops end
controller stops:
class stopscontroller < applicationcontroller # /loads/:load_id/stops/new def new load = load.find(params[:load_id]) @stop = load.stops.build end # post /loads/:load_id/stops def create load = load.find(params[:load_id]) @stop = load.stops.create(params[:stop]) if @stop.save format.html { redirect_to([@stop.post, @stop], :notice => 'stop created.') } else format.html { render :action => "new" } end end end
extracted here:
http://blog.8thcolor.com/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/
Comments
Post a Comment