【发布时间】: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