当我单击 upvote 时出现错误,请 在此处输入图像描述
这是我的路线.rb
Rails.application.routes.draw do
#root 'pages#index'
resources :posts
devise_for :users
# Define root URL
root 'pages#index'
# Define roots for pages
get '/home' => 'pages#home'
get '/user/:id' => 'pages#profile'
get '/explore' => 'pages#explore'
resources :posts do
member do
put "like", to: "posts#upvote" # maybe change it to POSTS??
put "dislike", to: "posts#downvote" # maybe change it to POSTS??
end
end
还有我的posts_controller.rb
class PostsController < ApplicationController
#resources posts
def new
@post = current_user.posts.build
end
def index
@post = Post.all
end
def show
@post = Post.find(params[:id])
end
def create
@user = User.find(session[:user_id])
@post = current_user.posts.build(permit_post)
if @post.save
flash[:success] = "Uploaded"
redirect_to '/'
else
flash[:error] = @posts_errors_full_messages
end
end
def upvote
current_user = User.find_by_id(session[:user_id])
@post = Post.find(params[:id])
current_user.upvotes @post
redirect_to :back
end
def downvote
@post = Post.find(params[:id])
@post.downvote_by current_user
redirect_to :back
end
private
def permit_post
params.require(:post).permit(:image, :title);
end
end
我的索引中有
<%= link_to like_post_path(@post), method: :put, class: 'upvote' do %>
在 URL 中显示 myurl.com/posts/%23/like 我认为这意味着它没有获得 id,我该如何解决?这真的让我筋疲力尽,提前谢谢
编辑:我用 x 替换了@post,因为我使用了一个循环,它修复了 url,但仍然得到相同的错误,即无法找到没有 ID 的帖子。