Reputation: 229
I am new to ruby and RoR. I am using the ruby gem acts_as_votable to let users like and unlike posts. I added a "like" button to all posts on my posts/index page.
The button displays correctly and I am trying to make a button click like/unlike the post it is displayed on. When I click the button, to like a post, I get this error:
Couldn't find Post with 'id'=#<Post::ActiveRecord_Relation:0x00000002ba3b30>
def find_post
@post = Post.find(params[:id])
end
BTW: If you need anymore information, I will provide whatever I can.
Index page
- @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
Posts controller
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
Routes
Rails.application.routes.draw do
devise_for :users
resources :posts do
member do
put "admire", to: "posts#upvote"
end
end
root "posts#index"
end
In terminal, I entered: rake routes. Here is the response:
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
Upvotes: 0
Views: 1187
Reputation: 2883
You didn't post your model, but let's test it like this:
1 - Open your rails console (type 'rails c' on the app dir), and write Post.first (Assuming you have posts), if that works the model should be fine.
2 - There must be something wrong with you params, change your find_post method to this:
def find_post
puts params
@post = Post.find(params[:id])
end
Check the output, if you see anything different than a number (id) your passing it wrong on the view.
Upvotes: 0
Reputation: 3074
Your link_to
in your index-view passes an ActiveRecord
-relation post
.
= link_to (image_tag post.image.url), post
Change it so you pass the post.id
as a parameter instead.
= link_to (image_tag post.image.url), post.id
Edit
You might even need to link it to something like post_path(post)
or any equivalent you have set in your routes.rb
. Then you don't need to set the id
and just pass the whole post
.
Upvotes: 0
Reputation: 2787
Hello buddy I think It's a logic error You are putting
before_action :find_post, only: [:show, :edit, :update, :destroy, :upvote]
So the action def index @post = Post.all.order("created_at DESC") end
where you are operating didn't find_post as you told only these actions to do so and even if :upvote is included the action index where all this is taking place isn't reading find_post. Try adding :index to your list and let us see the reaction.
Hope this is helpful and enough explanation.
Upvotes: 0