将多个参数传递到路由帮助程序链接中

passing multiple parameters into route helpers link

提问人:Thảo Nguyễn 提问时间:7/24/2023 更新时间:7/24/2023 访问量:21

问:

我是 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_touser/show.html.erblink_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

ruby-on-rails 参数 嵌套 资源 链接

评论

2赞 AbM 7/24/2023
您需要使用 代替 ,例如event_event_guest_pathevent_event_guests_pathevent_event_guest_path(@event.id, @event_guest)
0赞 Thảo Nguyễn 7/24/2023
@AbM天哪,谢谢你,简直不敢相信我犯了这么愚蠢的错别字

答: 暂无答案