开发者

Updating a 2-dimensional form

开发者 https://www.devze.com 2022-12-09 02:07 出处:网络
Hey guys. I\'m new-ish to rails development and have hit a bit of a wall. The application I\'m working on is a scheduling solution that requires updating a join model, but not in a simple 1:1 sort of

Hey guys. I'm new-ish to rails development and have hit a bit of a wall. The application I'm working on is a scheduling solution that requires updating a join model, but not in a simple 1:1 sort of way.

The app is laid out as follows:

class Route < ActiveRecord::Base
  has_many :markers, :foreign_key => 'source_id'  
  has_many :schedules

  accepts_nested_attributes_for :markers, :allow_destroy => true, :reject_if => proc { |a| a['name'].blank? }
  accepts_nested_attributes_for :schedules, :allow_destroy => true, :reject_if => proc { |a| a['name'].blank? }
end

class Schedule < ActiveRecord::Base  
  has_many :ar开发者_如何学JAVArivals
  has_many :markers, :through => :arrivals

  accepts_nested_attributes_for :arrivals, :allow_destroy => true, :reject_if => :all_blank
end

class Marker < ActiveRecord::Base  
  has_many :arrivals
  has_many :schedules, :through => :arrivals
end

class Arrival < ActiveRecord::Base
  belongs_to :marker
  belongs_to :schedule
end

... so a basic has_many :through ... or so I would think :P

When you create a route, you can create 1..n schedules, and 1..n markers. Editing a schedule should allow you to add 1..n arrival entries for each marker defined in the route. THIS is what's causing me grief.

Through the magic of ascii-art, this is what I want the app to look like:

/views/routes/edit.html.erb (works already)

ROUTE
-----
...

SCHEDULES
---------
[Add]

* Schedule 1 [Edit][Delete]
* Schedule 2 [Edit][Delete]
...

MARKERS
-------
[Add]

* Marker 1 [Edit][Delete]
* Marker 2 [Edit][Delete]
* Marker 3 [Edit][Delete]
* Marker 4 [Edit][Delete]
...

/views/schedules/edit.html.erb

SCHEDULE X
----------
[Add Col.]

Marker 1 [ ] [ ]
Marker 2 [ ] [ ]
Marker 3 [ ] [ ]
Marker 4 [ ] [ ]
         [x] [x]

(the [x] should remove a column)

EDIT (09NOV04):

I've removed the incomplete view code I originally had posted, but would like to update the question a bit.

I think part of the confusion here (for myself, and possibly for anyone who might be able to help) is that I haven't explained the relationships properly.

  1. markers have many arrivals
  2. schedules have many markers
  3. routes have many schedules

That's the basics.

Having a form that would update arrivals for a single marker wouldn't be difficult, as that's a basic form. What I'm hoping to do is to provide a form that updates all markers at the same time.

When you click on "Add Entry", it should add a new arrival for each marker that's currently available. Under each "column", there should be a "remove" button, that will remove each arrival for that particular column (so from each marker).

I'm not sure if that clears it up any :P


When you create a route, you can create 1..n schedules, and 1..n markers. Editing a schedule should allow you to add 1..n arrival entries for each marker defined in the route. THIS is what's causing me grief.

There is nothing linking markers to routes as far as schedules are concerned. The way you've laid things out any schedule can add any number arrival entries for each marker defined in your database.

You need make a few changes to get the functionality you want. I'm assuming that a schedule belongs_to a route. I've also left out the accepts_nested_attributes_for lines out, to conserve space.

class Route < ActiveRecord::Base
  has_many :markers, :foreign_key => 'source_id'  
  has_many :schedules
  ...
end

class Schedule < ActiveRecord::Base  
  has_many :arrivals
  belongs_to :route
  has_many :markers, :through => :arrivals
  has_many :route_markers, :through => :route, :source => :markers
  ...
end

class Marker < ActiveRecord::Base  
  has_many :arrivals
  has_many :schedules, :through => :arrivals
  ...
end

class Arrival < ActiveRecord::Base
  belongs_to :marker
  belongs_to :schedule
  ...
end

Now @schedule.route_markers returns a list of markers in the route linked to the schedule. You can use those to generate your grid. Then create arrival objects to establish a marker in a specific schedule.

Then it's just a matter of @schedule.markers= list_of_markers and rails takes care of creating/deleting entries in the join table.

Sorry but without knowing more, I'm not going to speculate on what the view will look like.

0

精彩评论

暂无评论...
验证码 换一张
取 消