我只贴相关代码: #event_attendees_controller.rb
class EventAttendeesController < ApplicationController
def edit
@attendee = @event.attendees.find(params[:id])
if @attendee == nil
throw "Attendee with id #{params[:id]} not found"
else
logger.info "Attendee with id #{params[:id]} is : #{@attendee.to_xml}"
end
end
private
def find_event
@event = Event.find(params[:event_id])
end
end
#event.rb
class Event < ActiveRecord::Base
has_many :event_attendees
def attendees
return event_attendees
end
end
#event_attendees/edit.html.erb
<%= form_for @attendee, :url => event_attendee_path(@event, @event_attendee), :method => :put do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
#event_attendees/index.html.erb
<ul>
<% @attendees.each do |attendee| %>
<li>
<%= attendee.name %>
<%= link_to "Show", event_attendee_path(@event, attendee)%>
<%= link_to "Edit", edit_event_attendee_path(@event, attendee) %>
<%= link_to "Delete", event_attendee_path(@event, attendee), :method => :delete %>
</li>
<% end %>
</ul>
<%= link_to "New Attendee", new_event_attendee_path(@event) %>
#event_attendee.rb
class EventAttendee < ActiveRecord::Base
attr_accessible :name
belongs_to :event
end
#routes.rb
resources :events do
resources :attendees, :controller => :event_attendees
end
当在 index 页面点击 Edit 按钮的时候提示: No route matches {:action=>"show", :controller=>"event_attendees", :event_id=>#, :id=>nil} 很奇怪为什么传过来的 event_attendee 的 id 是 nil,但通过调试发现 controller 里面的 id 不是 nil,各位大牛能解释解释吗?