【问题标题】:Favorite action getting error: Wrong number of arguments (given 1, expected 0)?最喜欢的操作出现错误:参数数量错误(给定 1,预期为 0)?
【发布时间】:2016-11-03 14:12:37
【问题描述】:

我有一个工作最喜欢的操作,我的用户可以喜欢一个房间,但现在我收到了这个错误,我不知道为什么它不起作用?

我怎样才能完成这项工作?

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

【问题讨论】:

    标签: ruby-on-rails view controller arguments


    【解决方案1】:

    我认为您正在寻找的内容包括:

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

    【讨论】:

    • @trickydiddy 没有问题!如果它对你有用,请确保接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多