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])
Related
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?
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 %>
please help solve the problem.
a resource documents and the appropriate controller:
class DocumentsController < ApplicationController
def index
#documents = Document.all.paginate(page: params[:page], per_page: 10)
end
def admin_index
#documents = Document.all.paginate(page: params[:page], per_page: 10)
render layout: "admin"
end
def show
#document = Document.find(params[:id])
end
def admin_show
#document = Document.find(params[:id])
render layout: "admin"
end
....
....
end
a 2 layouts:
application.html.erb,
admin.html.erb
the controller index lists the documents in the public section (application.html.erb).
admin_index controller displays a list of documents in the closed part of the site (admin.html.erb).
in the public part of the site I can look any document by clicking on 'show':
<% #documents.each do |document| %>
<%= document.title %>
<%= link_to 'Show', document %>
<% end %>
The problem is that in a closed part of the site I did not get to see any document by clicking the link below:
<%= link_to 'Show', document %>
a problem that throws me a page of a particular document, but the layout: application.html.erb, and I need a layout: admin.html.erb
routes:
Testpager::Application.routes.draw do
get "admin/index"
resources :news, only: [:index, :show]
resources :documents, only: [:index, :show, :destroy]
get "contacts/index"
get "services/index"
get "index/index"
get "admin/index"
get "admin/documents" => 'documents#admin_index'
get "admin/documents/:id" => 'documents#admin_show'
root 'index#index'
end
First of all, there's no difference in the link you've created with the link_to helper in your index.html.erb and admin_index.html.erb, so let's address that first.
Change your admin_show route in your routes.rb to:
get 'admin/documents/:id', to: 'documents#admin_show', as: 'admin_document'
Now change the link_to in your admin_index.html.erb to this:
<%= link_to 'Show', admin_document_path(document) %>
That should do it.
Side note
The way you've set up the 'public' and 'admin' parts of your site seem odd. I would personally create an admin namespace and a separate DocumentsController in that namespace. You'll have to change your routes.rb, create an admin controller, view and layout if you want this.
Add the documents resource to the admin namespace instead of the admin routes you've already added:
...
namespace :admin do
resources :documents, only: [:index, :show, :destroy]
end
resources :documents, only: [:index, :show, :destroy]
...
Create a app/controllers/admin/documents_controller.rb file and move the admin_* methods from your original controller to that one and also move the admin_*.html.erb views to app/views/admin/*.html.erb.
Now, last but not least move your app/views/layouts/admin.html.erb file to app/views/layouts/admin/application.html.erb. That should do it.
Note that you've got to use a module in your admin controller:
module Admin
class DocumentsController < ApplicationController
...
end
end
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.