2
我有下面許多人在許多Rails的關係刪除網址:嵌套協會(當然的ActiveResource)
class User < ...
has_many :channel_assignments
has_many :channels, :through => :channel_assignments
end
class Channel < ...
has_many :channel_assignments
has_many :users :through => :channel_assignments
end
class ChannelAssignment < ...
belongs_to :user
belongs_to :channel
end
定義的路由:
map.resources :users, :has_many => :channel_assignments
更新:耙路線給出以下輸出:
user_channel_assignments GET /users/:user_id/channel_assignments(.:format) {:action=>"index", :controller=>"channel_assignments"}
POST /users/:user_id/channel_assignments(.:format) {:action=>"create", :controller=>"channel_assignments"}
new_user_channel_assignment GET /users/:user_id/channel_assignments/new(.:format) {:action=>"new", :controller=>"channel_assignments"}
edit_user_channel_assignment GET /users/:user_id/channel_assignments/:id/edit(.:format) {:action=>"edit", :controller=>"channel_assignments"}
user_channel_assignment GET /users/:user_id/channel_assignments/:id(.:format) {:action=>"show", :controller=>"channel_assignments"}
PUT /users/:user_id/channel_assignments/:id(.:format) {:action=>"update", :controller=>"channel_assignments"}
DELETE /users/:user_id/channel_assignments/:id(.:format) {:action=>"destroy", :controller=>"channel_assignments"}
由於ChannelAssignemnts綁定對於用戶,我使用腳手架ChannelAssignmentsController在創建ChannelAssignment時自動將用戶分配到頻道。
我這樣做,通過使用這些URL:
#/app/views/users/index.html.erb
#show a link to view all channels of a user
<%= link_to 'Channels', user_channel_assignments_path(user) %>
...
#/app/views/channel_assignments/new.html.erb
#assign a channel to currently selected user
<% form_for(@channel_assignment, :url => user_channel_assignments_path(@user)) do |f| %>
...
那迷人的作品。
但是:哪個是取消分配頻道的路徑,ergo:刪除用戶的ChannelAssignment?運行耙路線時找不到它。
必須像
<%= link_to 'Destroy', delete_user_channel_assignment, :user_id => @user, :method => :delete %>
任何輸入這個?我確定有一種方法可以自動生成此URL。
感謝
馬特
嗨馬特,感謝您的回答,但這並沒有訣竅。我用耙子路線的輸出更新了我的帖子。您的代碼會生成一個很好的Javascript表單,但會忽略user_id。之前也有過。任何其他建議? Matt – Matt 2009-06-22 13:23:31