提问人:Thảo Nguyễn 提问时间:7/24/2023 更新时间:7/24/2023 访问量:21
将多个参数传递到路由帮助程序链接中
passing multiple parameters into route helpers link
问:
我是 Rails 的新手。我正在尝试建立一个网站。以下是我的数据模型:
- user:
has_many :created_events, foreign_key: "creator_id", class_name: "Event"
has_many :guest_events, foreign_key: "attendee_id"
has_many :attended_events, through: :guest_events
- event:
belongs_to :creator, class_name: 'User'
has_many :event_guests, foreign_key: "attended_event_id"
has_many :attendee, through: :event_guests
- event_guest:
belongs_to :attendee, class_name: "User"
belongs_to :attended_event, class_name: "Event
我希望我的用户能够参加一个活动,所以我在 .如果他们已登录并参加该活动,则可以通过单击视图中的其他帮助者来选择不再参加该活动。link_to
user/show.html.erb
link_to
我的问题是当用户单击“不参加”链接时,我遇到以下错误:
No route matches [GET] "/events/7/event_guests.4"
config/routes.rb
文件:resources :events do resources :event_guests, only: [:create, :destroy] end resource :user do resources :events end
Event_guests路线:
Prefix Verb URI Pattern Controller#Action event_event_guests POST /events/:event_id/event_guests(.:format) event_guests#create event_event_guest DELETE /events/:event_id/event_guests/:id(.:format) event_guests#destroy
EventsController
:def show @event = Event.find(params[:id]) @event_guest = EventGuest.where("attended_event_id = ? AND attendee_id = ?", @event.id, current_user.id) if @event_guest.exists? @event_guest = @event_guest.take else @event_guest = nil end end
EventGuestsController
:def destroy if EventGuest.exist?(params[:id]) @event_guest = EventGuest.find(params[:id]) @event_guest.destroy end redirect_to root, status: :see_other end
link_to
以下助手:user/show.html.erb
<%= link_to "Attend", event_event_guests_path(@event), data: { turbo_method: "post" } %> <% if user_signed_in? && !@event_guest.nil? %> <%= link_to "Not attend", event_event_guests_path(@event, @event_guest), data: {turbo_method: "delete" } %> <% end %>
在助手中,我尝试了:link_to "Not attend"
event_event_guests_path(@event.id, @event_guest.id)
event_event_guests_path(@event, :id => @event_guest.id)
event_event_guests_path(:event_id => @event.id, :id => @event_guest.id)
但他们都没有奏效。我曾经对用户进行身份验证。
请帮帮我!谢谢。devise
答: 暂无答案
下一个:如何限制嵌套模型总数?
评论
event_event_guest_path
event_event_guests_path
event_event_guest_path(@event.id, @event_guest)