我有一个带有bookings#index操作的名为BookingsController的控制器。在索引操作中,有两个实例变量@pending_bookings
和@approved_bookings
,它们通过status
查询预订对象。
def index
@pending_bookings = Booking.where(host_id:@user.id,
status:'pending')
@approved_bookings = Booking.where(host_id:@user.id,
status:'approved')
end
我希望根据用户单击的链接将用户路由到索引的不同实例。基本上,bookings_path(@pending_bookings)
应该将用户路由到显示所有pending_bookings
的索引页面,相反,bookings_path(@approved_bookings)
应该将用户路由到显示所有approved_bookings
的索引页面。
在我看来,我有2个链接,应该分别将用户定向到每个路径。
<%= link_to 'Pending Reservations', bookings_path(@pending_bookings)%>
<%= link_to 'Approved Reservations', bookings_path(@approved_bookings)%> `
index.html.erb文件:
<%= booking_index_helper_path %>
包含一个嵌入的帮助器方法,它应该识别用户单击的路径并呈现适当的预订对象。
下面是识别用户选择的路径并渲染必要对象的(有缺陷的)逻辑:
pages_helper.rb:
def booking_index_helper_path
if bookings_path(@pending_bookings)
render @pending_bookings
elsif bookings_path(@approved_bookings)
render @approved_bookings
else bookings_path(@total_bookings)
@total_bookings
end
end
我在帮助器方法中放入了一个binding.pry
,以确认它正在被命中(确实如此)。但是,由于某些原因,当我单击链接以指向适当的对象时,总是满足第一个条件。编写此条件以识别用户选择的路径的更好方法是什么?
https://stackoverflow.com/questions/56761039
复制相似问题