Ok... so now I'm at day 2 trying to figure out why on my development env, it works but when I deploy my app to production it doesn't work.
I'm simply trying to create a "like/upvote" button and count the "likes/upvotes" on every post, and then do the same for every comment in a post.
Since I'm using friendly_id to create a slug from the :title field, I'm getting this in my Heroku Logs:
ActionController::RoutingError (No route matches [GET] "/posts/test/upvote"):
I obviously get the "This page doesn't exist" error...
Here's the code:
routes.rb
resources :posts, only: [:create, :index, :show, :destroy, :edit, :update] do
resources :comments, only: [:show, :create, :destroy, :edit, :update] do
member do
put '/upvote' => 'comments#upvote'
end
end
member do
put '/upvote' => 'posts#upvote'
end
end
end
post_controller.rb
def upvote
#post = Post.friendly.find(params[:id])
#post.increment!(:upvotes)
redirect_to root_path
end
comments_controller.rb
def upvote
#post = Post.friendly.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.increment!(:upvotes)
redirect_to #post
end
post index.html.erb
<%= link_to "like", upvote_post_path(post), method: :put, style: "color: white;" %>
What's missing here that I'm not seeing?
Related
I've done Michael Hartl Ruby on Rails 5 tutorial and am now trying to apply the code to my own app.
What I'm trying to do is:
Follow/Unfollow an event
Display total count of users following this event
Users can see all the events that they are following
The current error appears when I try to render the conferences that current user is following.
The error that I get is:
ActionController::UrlGenerationError in Users#show
No route matches {:action=>"followers", :controller=>"conferences", :id=>nil} missing required keys: [:id]
The line that causes the problem is:
<a href="<%= followers_conference_path(#conference) %>">
Now obviously their is something wrong in my routing and I assume the following problem states that conferences is missing a required id?
Does anyone know the solution to my problem? Which is getting allowing users to follow events and see what events they are following
USER CONTROLLER SHOW
def show
#user = User.find(params[:id])
#microposts = #user.microposts.paginate(page: params[:page])
#managments = #user.managments.paginate(page: params[:page])
#conference = Conference.find(params[:id])
end
ROUTES.RB
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/my_conference', to: 'my_conference#show'
get '/conferences', to: 'conferences#index'
get '/conferences_list', to: 'conferences#index_admin'
get '/my_employees', to: 'employees#index'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/login', to: 'sessions#new'
put 'activate/:id(.:format)', :to => 'users#activate', :as => :activate_user
put 'deactivate/:id(.:format)', :to => 'users#deactivate', :as => :deactivate_user
put 'activate_employee/:id(.:format)', :to => 'employees#activate', :as => :activate_employee
put 'deactivate_employee/:id(.:format)', :to => 'employees#deactivate', :as => :deactivate_employee
resources :users do
member do
get :following
end
end
resources :conferences do
member do
get :followers
end
end
resources :articles
resources :users
resources :employees
resources :account_activations, only: [:edit]
resources :activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :managments
resources :conferences
resources :relationships, only: [:create, :destroy]
end
CONFERENCES_CONTROLLER.RB
class ConferencesController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
before_action :admin_user, only: :destroy
def index
#conferences = Conference.paginate(page: params[:page])
if params[:search]
#conferences = Conference.search(params[:search]).order("created_at DESC").paginate(page: params[:page])
else
#conferences = Conference.all.order('created_at DESC').paginate(page: params[:page])
end
end
def new
#user = User.new
#conference = Conference.new
end
def create
#conference = current_user.conferences.build(conference_params)
if #conference.save
flash[:success] = "conference created!"
redirect_to conferences_path
else
#feed_items = current_user.feed.paginate(page: params[:page])
render 'new'
end
end
def destroy
#conference.destroy
flash[:success] = "conference deleted"
redirect_to request.referrer || root_url
end
def following
#title = "Following"
#conference = Conference.find(params[:id])
#conferences = #conference.following.paginate(page: params[:page])
render 'show_follow'
end
def followers
#title = "Followers"
#user = User.find(params[:id])
#users = #user.followers.paginate(page: params[:page])
render 'show_follow'
end
private
def conference_params
params.require(:conference).permit(:conference,:country , :month, :presence, :audience, :cost ,:picture)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def correct_user
#conference = current_user.conferences.find_by(id: params[:id])
redirect_to root_url if #conference.nil?
end
end
Ahh, i see the issue.
Check your UsersController#show to see if #conference is being set anywhere.
If not try: <a href="<%= followers_conference_path(current_user.id) %>">
You don't want to use the user id to get the #conference object, I'm sure. Instead, you are probably generating a list of conferences the user is following. In that case, you'd use something like this
<% #user.conferences.each do |conference| %>
<%= conference.name %>
<% end %>
And, in Rails, you would usually use the link_to helper, and maybe include a count to show how many followers the conference has.
<% #user.conferences.each do |conference| %>
<%= link_to conference.name, followers_conference_path(conference) %>
(<%= pluralize conference.users.count, 'follower' %>)
<% end %>
I am trying to de-active user from my rails application. However, this is not working at the moment.
My routes file -
# Root is the unauthenticated path
root 'sessions#unauth'
# Sessions URL
get 'sessions/unauth', to: 'sessions#unauth', as: :login
post 'sessions/login', as: :signin
delete 'sessions/logout', as: :logout
# Resourceful routes for articles
resources :articles
get '/interests', to: 'articles#my_interests', as: 'interests'
get '/destroy', to: 'users#destroy', as: 'destroy_user'
resources :users, only: [:create,:new,:update,:destroy,:edit]
Then I have a html file inside layout folder.
<li><%= link_to "De-activate User", destroy_user_path(current_user)%></li>
User will be clicking De-active User button, and I am expecting action to go into my Users Controller. Below is my UsersController.rb.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user, only: [:edit, :destroy, :update]
before_action :check_valid, only: [:edit, :destroy, :update]
# DELETE /users/1
# DELETE /users/1.json
def destroy
log_out #user
#user.destroy
respond_to do |format|
format.html { redirect_to login_path, notice: 'user was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
def check_valid
unless #user==current_user
redirect_to articles_path
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :bio, :username, :password, :interest_list, :password_confirmation)
end
end
Rails give me error in the set_user method.
Error - Couldn't find User with 'id'= at #user = User.find(params[:id])
I am not able to understand what is the issue over here?
My log_out method -
def log_out
session[:user_id] = nil
end
Try this
<li><%= link_to "De-activate User", destroy_user_path(current_user), method: :delete%></li>
Thanks
You have override your default path that generated by resources :users with:
get '/destroy', to: 'users#destroy', as: 'destroy_user'
If you properly see your built route you can not find params[:id]. With this route you can go to action but could not find params[:id] there.
You can change your route like this :
get '/destroy/:id', to: 'users#destroy', as: 'destroy_user'
So your cuurent code will work properly.
Please try this:
<%= link_to "De-activate User", current_user, method: :delete %>
OR
<%= link_to "De-activate User", user_path(current_user), method: :delete %>
So I just made one of my objects a nested resource of my user object. Now all of my links don't work and my index won't show. I am getting the error:
/views/photos/index.html.haml where line #8 raised:
No route matches {:action=>"show", :controller=>"photos", :id=>nil,
:user_id=>nil} missing required keys: [:id, :user_id]
with this line being where its finding the issue:
= link_to photo.title, user_photo_path(#user, #photos)
This is my controller for my photos object:
class PhotosController < ApplicationController
before_action :find_photo, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
def index
#photos = Photo.all.order(:cached_weighted_score => :desc)
end
def show
#comments = Comment.where(photo_id: #photo)
#photo = Photo.find(params[:id])
end
My routes look like this:
Rails.application.routes.draw do
devise_for :users
root 'photos#index'
resources :users do
resources :photos do
member do
get "like", to: "photos#upvote"
get "unlike", to: "photos#downvote"
end
resources :comments
end
end
end
This is my user controller:
class UsersController < ApplicationController
def show
#user = User.find_by(params[:id])
#photos= #user.photos.order(:cached_weighted_score => :desc)
end
end
finally the view that is generating the error code:
.container
.row
.col-lg-12
%h1.page-header Most Popular Photos
- #photos.each do |photo|
.thumbnail.col-lg-3.col-md-4.col-xs-6.thumb
%h2
= link_to photo.title, user_photo_path(#user, #photo)
= link_to (image_tag photo.image.url(:small)), photo
%p
= photo.get_likes.size
Likes
= link_to "Add New Photo", new_photo_path
Any help is appreciated. My last change was adding the photos route into below the users route.
2nd arg needs to be a photo instance from the loop not #photo which is probably nil:
= link_to photo.title, user_photo_path(#user, photo)
UPDATE 1: You also need to load up #user in PhotosController#show:
#user = User.find_by(params[:user_id])
I am still very much new to rails, but cant seem to get a grasp on this route
show.html.erb
<%= link_to "up", vote_movie_review_path(#review, type: "up"), method: "post" %>
rake route
vote_movie_review POST /movies/:movie_id/reviews/:id/vote(.:format) reviews#vote
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :movies do
resources :reviews do
member { post :vote }
end
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_reviews, only: [:show, :edit, :update, :destroy]
before_action :set_movie
before_action :authenticate_user!
respond_to :html
def index
#reviews = Review.all
respond_with(#reviews)
end
def show
end
def vote
value - params[:type] == "up" ? 1 : -1
#review = Review.find(params[:id])
#review.add_evaluation(:votes, value, current_user)
redirect_to :back, notice: "thanks for the vote"
end
You are using nested routes, so you need to pass movie object also.use like this vote_movie_review_path(#movies, #review, type: "up").
Check your routes, it showing /movies/:movie_id/reviews/:id/vote while the way you are calling it will generate like /reviews/id with method post and for it you have not defined any routes.
I am writing an application , which User can create Topics and others can make posts on that topic.
I am stuck with this error :
No route matches {:action=>"show", :controller=>"topics", :id=>nil}
my route.rb :
MyPedia2::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics, only: [:show, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/topics/:id', to: 'topics#show'
my rake route shows :
topics POST /topics(.:format) topics#create
topic GET /topics/:id(.:format) topics#show
DELETE /topics/:id(.:format) topics#destroy
root / static_pages#home
/topics/:id(.:format) topics#show
and my topics controller is:
# encoding: utf-8
class TopicsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def show
#topic = Topic.find_by_id(params[:id])
end
def create
#topic = current_user.topics.build(params[:topic])
if #topic.save
flash[:success] = "Konu oluşturuldu!"
redirect_to root_path
else
render 'static_pages/home'
end
end
def destroy
#topic.destroy
redirect_to root_path
end
private
def correct_user
#topic = current_user.topics.find_by_id(params[:id])
redirect_to root_path if #topic.nil?
end
end
Is there a fix for this ?
EDIT : I found that _topics.html.erb fails
I found what breakes the code :
<% for topic in #topics do %>
<li><%=link_to topic.title, topic_path(#topic) %></li>
<%= will_paginate #topics %>
<% end %>
topic_path(#topic] part is wrong. How can i make it to use id?
It's not working because your collection is '#topics', and each element is 'topic', not '#topic'. But you're close. Try this:
<li><%=link_to topic.title, topic_path(topic) %></li>
Try this:
<li><%=link_to topic.title, topic_path(:id => #topic.id) %></li>
I think your routes should probably read:
resources :sessions, :only => [:new, :create, :destroy]
resources :topics, :only => [:show, :create, :destroy]
After hours of thinking , now i can see my mistake. I used show method in my topics controller but i did not have show.html.erb in my views/topics.
If you want to show your topics you must use these methods:
1) in config/routes.rb use :
match '/topics/:id', to: 'topics#show'
2) in the model i used
belongs_to :user,:foreign_key => "user_id"
3) link as :
<li><%=link_to topic.title, **topic_path(topic)** %></li>
4) and prepare the template you mentioned in the route.
I hope this help anyone.