繁体   English   中英

最喜欢的操作发生错误:参数数量错误(给定1,预期为0)?

[英]Favorite action getting error: Wrong number of arguments (given 1, expected 0)?

我有一个喜欢的工作方式,我的用户可以喜欢一个房间,但现在却出现此错误,我不知道为什么它不起作用?

我该如何进行这项工作?

在此处输入图片说明

show.html.erb

   <% if current_user && current_user.favorites.exists(@room) %>
      <%= link_to "unfavorite", favorite_room_path(@room, type: "unfavorite"), method: :put %>
   <% else %>
      <%= link_to "favorite", favorite_room_path(@room, type: "favorite"), method: :put %>
   <% end %>

rooms_controller.rb

  before_action :set_room, only: [:show, :favorite]
  before_action :authenticate_user!, only: [:favorite]

  def show
    @photos = @room.photos

    @booked = Reservation.where("room_id = ? AND user_id = ?", @room.id, current_user.id).present? if current_user

    @reviews = @room.reviews
    @hasReview = @reviews.find_by(user_id: current_user.id) if current_user
  end

  def favorite
    type = params[:type]
    if type == "favorite"
      current_user.favorites << @room unless current_user.favorites.exists?(@room)
      redirect_to wishlist_path, notice: 'You favorited #{@room.listing_name}'
    elsif type == "unfavorite"
      current_user.favorites.delete(@room)
      redirect_to wishlist_path, notice: 'Unfavorited #{@room.listing_name}'
    else
      # Type missing, nothing happens
      redirect_to wishlist_path, notice: 'Nothing happened.'
    end
  end


  private
    def set_room
      @room = Room.find(params[:id])
    end

我认为您要寻找的包括:

if current_user && current_user.favorites.include?(@room)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM