我的模型
class Client < ActiveRecord::Base
attr_accessible :name
has_many :bookings
validates_presence_of :name
end
class Agent < ActiveRecord::Base
attr_accessible :name
has_many :bookings
validates_presence_of :name
end
class Booking < ActiveRecord::Base
attr_accessible :booking_time, :agent_id
belongs_to :client
belongs_to :agent
validates_presence_of :booking_time
end
这让我很头疼。我希望从代理和客户双方查看预订,但是预订控制器的索引方法如何处理路线?
agents/agent_id/bookings and clients/client_id/bookings
?
第二个问题:只有客户创建预订,但我如何正确维护预订和代理之间的关系?
def create
@client = Client.find(params[:client_id])
@booking = @client.bookings.build(params[:booking])
@agent = Agent.find(params[:booking][:agent_id])
@agent.bookings << @booking
if (@booking.save and @agent.save)
redirect_to [@client, @booking]
else
render :action => "new", :notice => "Booking could not be created"
end
end