我是ruby和RoR的新手。我正在使用ruby gem acts_as_votable来让用户喜欢和不喜欢帖子。我添加了一个"喜欢"在我的帖子/索引页面上按所有帖子。
按钮显示正确,我正在尝试点击按钮/与其显示的帖子不同。当我点击按钮,喜欢帖子时,我收到此错误:
Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x00000002ba3b30>
def find_post
@post = Post.find(params[:id])
end
顺便说一句:如果您需要更多信息,我会尽我所能。
索引页
- @post.each do |post|
.box.panel.panel-default
= link_to (image_tag post.image.url), post
.post-actions
= render "posts/actions"
.panel-body
%h2= link_to post.caption, post
帖子控制器
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy, :upvote]
before_action :authenticate_user!, except: [:index, :show]
def index
@post = Post.all.order("created_at DESC")
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post, notice: "Success! Your new post has been created."
else
render 'new'
end
end
def show
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post, notice: "Success! You updated your post."
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
def upvote
@post.upvote_by current_user
redirect_to :back
end
private
def post_params
params.require(:post).permit(:caption, :image)
end
def find_post
@post = Post.find(params[:id])
end
end
路线
Rails.application.routes.draw do
devise_for :users
resources :posts do
member do
put "admire", to: "posts#upvote"
end
end
root "posts#index"
end
在终端,我输入了:rake路线。以下是回复:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
admire_post PUT /posts/:id/admire(.:format) posts#upvote
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index
答案 0 :(得分:0)
你没有发布你的模型,但让我们这样测试:
1 - 打开你的rails控制台(在app dir上键入&#39; rails c&#39;),然后写Post.first(假设你有帖子),如果有效,模型应该没问题。
2 - 你的params一定有问题,把你的find_post方法更改为:
def find_post
puts params
@post = Post.find(params[:id])
end
检查输出,如果你看到的不同于数字(id),你在视图上传递错误。
答案 1 :(得分:0)
Hello buddy我认为这是一个逻辑错误你正在放
before_action:find_post,only:[:show,:edit,:update,:destroy,:upvote]
所以行动 def指数 @post = Post.all.order(“created_at DESC”) 端
你正在操作的地方没有find_post,因为你只告诉这些行为,即使:upvote包含在所有这一切发生的动作索引中不是读取find_post。 尝试添加:索引到您的列表,让我们看看反应。
希望这是有用且充分的解释。
答案 2 :(得分:0)
您的索引视图中的link_to
传递ActiveRecord
- 关系post
。
= link_to (image_tag post.image.url), post
更改它,以便将post.id
作为参数传递。
= link_to (image_tag post.image.url), post.id
修改的
您甚至可能需要将其与post_path(post)
或您在routes.rb
中设置的任何等效内容相关联。然后,您不需要设置id
,只需传递整个post
。