简体   繁体   中英

How can I display only a range of values in a rails form view?

I'm quite new to rails and I have some problem with views...
I have among others these three models : reservation , route and stop .
When you make a reservation, you have to select the route and the city of departure/arrival.
EDIT

class Route < ActiveRecord::Base
  has_many :stops, :dependent => :destroy
  has_many :reservations, :dependent => :destroy
  belongs_to :train
end.

    class Stop < ActiveRecord::Base
  belongs_to :route
  has_many :reservation_deps, class_name: "Reservation", foreign_key: "dep_city_id"
  has_many :reservation_arrs, class_name: "Reservation", foreign_key: "arr_city_id"
end

class Reservation < ActiveRecord::Base
  belongs_to :user
  belongs_to :route
  belongs_to :arr_city, class_name: "Stop",foreign_key: "arr_city_id"
  belongs_to :dep_city, class_name: "Stop",foreign_key: "dep_city_id"
end

I don't know if it is possible but I'd like to be able to select my city of departure/arrival only among those that belong to the route I've chosen. Any ideas how to do that?

Thank you!

Below there's a part of my reservation's form:

<div class="field">
      <%= f.label :route %><br>
    <%= f.collection_select(:route_id,Route.all,:id,:as_field) %><br>
  </div>
  <div class="field">
      <%= f.label :departure_city %><br>

      <%= f.collection_select(:dep_city_id,Stop.all,:id,:city) %><br>
  </div>
  <div class="field">
      <%= f.label :arrival_city %><br>

      <%= f.collection_select(:arr_city_id,Stop.all,:id,:city) %><br>
  </div>

Your stops should be linked to your routes through a has_and_belongs_to or has_many :through relationship right, since routes can contain many stops, and stops can belong to many routes. So assuming you have a route loaded on your reservations page, you should be able to just do the following

<%= f.collection_select :dep_city_id, @route.stops.all, :id, :name %>
<%= f.collection_select :arr_city_id, @route.stops.all, :id, :name %>

Alternatively something like

Edit: This will work even if the route to stops relationship is one to many.

<%= f.collection_select :dep_city_id, Stop.for_route(params[:route_id]), :id, :name %>

Edit 2:

In this case, it is expected that you have added a for_route scope to your Stop model which would just be a

scope for_route -> (route_id) { where(:route_id => params[:route_id]) }

In the basic case, you would have a page with your routes, you pick a route (link or something), and are taken to a page where you can make a reservation. The first page would have something like this

<% @routes.each do |route| %>
  <%= link_to route.name, new_reservation_path(:route_id => route.id) %>
<% end %>

The links generated by the code above should link to something like /reservation?route_id=XXX

When the reservations_controller#new is run, you know what the route ID is, so you can load it with something like @route = Route.find(params[:route_id])

Then, when rendering the page you will be able to use the f.collection_select helper I specified in the first example code.

In the last case, where you dont know what route the user will pick until they have selected one on the reservations page, it gets more complicated, and you will have to start using JS to query get a list of stops a route every time a new route is chosen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM