Why do I get the following, "undefined method `micropost_path'" in StaticPagesController , error, even though I copied Hartl's code verbatim?
Below is some of the code.
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :edit]
end
StaticPages Controller
class StaticPagesController < ApplicationController
def home
if logged_in?
#micropost = current_user.microposts.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
MicropostsController
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
#micropost.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
#micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if #micropost.nil?
end
end
Two of the answers, on this website, to this issue were solved for other reasons.
For example writing
micropost
instead of
microposts
I don't have these grammatical errors here if I am not mistaken.. Any idea what other problem could be causing this?
P.S. Using cloud9 IDE while following tutorial.
In routes.rb I had the following resources for microposts
resources :microposts, only: [:create, :edit]
Instead,it is supposed to be
resources :microposts, only: [:create, :destroy]
The
:destroy
was missing.
Found this out after running 'rake routes'..
got this route
edit_micropost GET /microposts/:id/edit(.:format) microposts#edit
instead of
micropost DELETE /microposts/:id(.:format) microposts#destroy
This shows the routes with their corresponding actions that should be in the controller.
Related
I am trying to create a log out for my app and in my controller this piece of code is not working. Any solution would be helpful.
def destroy
#user = user.find(params[:id])
#user.destroy
end
This is my destroy method in my sessions controller(the fact that i am destroying my user in my sessions controller might be the problem)
class UsersController < ApplicationController
def new
end
def create
#user = User.create(password: params[:password],
email: params[:email],
firstname: params[:firstname],
lastname: params[:lastname])
redirect_to user_path(#user)
end
def show
#user = User.find(params[:id])
end
end
routes.rb:
Rails.application.routes.draw do
# Fubyonrails.org/routing.html
root :to => 'static#welcome'
resources :users, only: [:new, :create, :show]
resources :session, :only => [:new, :create, :destroy]
resources :studios
end
Routes file
uninitialized constant SessionController
is the error im getting
First, don't destroy your user. It deletes it from the database, you want them to be able to login again without creating a new user right?
You need to update your routes.rb file to be:
Rails.application.routes.draw do
# Fubyonrails.org/routing.html
root :to => 'static#welcome'
resources :users, only: [:new, :create, :show]
resources :sessions, :only => [:new, :create, :destroy] # changed to sessions
resources :studios
get 'logout' => 'sessions#destroy'
end
If you don't already, you need to define a SessionsController.
class SessionsController < ApplicationController
def destroy
# insert logic that actually logs them out, the below may already be enough
# for this purpose though
redirect_to root_url, notice: "Logged Out Successfully"
end
end
When the user clicks "Logout" it gets routed to the SessionsController#destroy action which redirects them to the root URL ('static#welcome')
There is more to it then that (i.e. the views and all that jazz) but this should be helpful enough
in routes.rb i have
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get 'account_activation/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'sign_up' => 'user#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
post 'sign_up', to: 'user#create'
resources :user
resources :account_activation, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :purchases, only: [:create, :destroy]
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
i define in user_controller
class UserController < ApplicationController
before_action :logged_in_user, only: [:show, :edit, :update]
before_action :correct_user, only: [:show, :edit, :update]
def show
#user = User.find_by(id: params[:id])
#purchases = #user.purchases.paginate(page: params[:page])
#purchase = current_user.purchases.build
end
def index
if logged_in?
redirect_to root_url
else
redirect_to sign_up_path
end
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
#user.send_account_activation_email
flash[:info] = 'check'
redirect_to root_url(#user)
else
render 'new'
end
end
def edit
end
def chart
end
def update
if #user.update_attributes(user_params)
flash[:success] = 'updated'
redirect_to #user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def correct_user
#user = User.find_by(id: params[:id])
redirect_to(root_url) unless current_user?(#user)
end
end
both of them have a view but when i type localhost:300/user/(ID)/chart in browser rails tells me no route matches found but user/(ID)/edit works
when i use rake routes
chart do not shows in routes
i see MHartl rubyonrails tutorial screen cast
he do everything like this
where is the problem?!
resources :user do
member do
get :chart
end
end
define chart action like that
read routes in rails guide and check member and collection
resources :user creates only 7 default routes check 2.2 http://guides.rubyonrails.org/routing.html
I am up to chapter 11 of Rails Tutorial .org and am trying to get the 'Follow' and 'Unfollow' to work. When I go to click on either of these buttons all I get is the following error:
Routing Error
undefined method `filter' for RelationshipsController:Class
Try running rake routes for more information on available routes.
Here are the files:
relationships_controller.rb
class RelationshipsController < ApplicationController
before filter :signed_in_user
def create
#user = User.find(params[:relationship][:followed_id])
current_user.follow!(#user)
redirect_to #user
end
def destroy
#user = Relationship.find(params[:id]).followed
current_user.unfollow!(#user)
redirect_to #user
end
end
routes.rb
SampleApp::Application.routes.draw do
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
root :to => 'static_pages#home'
match '/help', to: "static_pages#help"
match '/about', to: "static_pages#about"
match '/contact', to: "static_pages#contact"
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy'
Your code says before filter (notice the space) when it should be before_filter.
(I'm french)
I've a problem with my Rails (3.2.11) application. When i set my routes with some nouns, it doesn't work !
I explain:
Way::Application.routes.draw do
scope(:path_names => { :new => "nouveau", :edit => "edition" }) do
scope "/admin" do
resources :news, except: [:show, :index], path: '/articles'
resources :users, except: [:show]
resources :pages, except: [:show]
resources :events, except: [:show, :index]
resources :projects, except: [:show,:index], path: '/projets'
resources :galleries, except: [:index, :show] do
resources :paintings, except: [:index, :show]
end
end
end
end
The resources :projects doesn't work when i set it to "/projets".
What doesn't work is: when i want to create a new project in my form, i click on submit, it simply redirect me to "/projets", without doing something !
But when i set the route to "/poneys" for example, it works ! I really don't understand.
Thanks for your help.
https://github.com/khcr/way
class ProjectsController < ApplicationController
def new
#project = Project.new
render layout: 'admin'
end
def create
#project = Project.new(params[:project])
if #project.save
flash[:success] = "Projet créé"
redirect_to project_path(#project)
else
render 'new', layout: 'admin'
end
end
end
I've been receiving the following error in my RSpec when trying to get my upvote method to pass:
Failures:
1) VotesController#up_vote adds an up-vote to the post
Failure/Error: post( :up_vote, post_id: #post.id )
ActionController::UrlGenerationError:
No route matches {:action=>"up_vote", :controller=>"votes", :post_id=>"1"}
Now I can upvote and downvote when I'm on the server, it's just my test that won't work.
Here's my code:
votes_controller_spec.rb
require 'rails_helper'
describe VotesController do
include TestFactories
include Devise::TestHelpers
describe '#up_vote' do
it "adds an up-vote to the post" do
request.env["HTTP_REFERER"] = '/'
#user = authenticated_user
#post = associated_post
sign_in #user
expect {
post(:up_vote, post_id: #post.id)
}.to change { #post.up_votes }.by 1
end
end
end
votes_controller.rb
class VotesController < ApplicationController
before_action :load_post_and_vote
def up_vote
update_vote!(1)
redirect_to :back
end
def down_vote
update_vote!(-1)
redirect_to :back
end
def update_vote!(new_value)
if #vote
authorize #vote, :update?
#vote.update_attribute(:value, new_value)
else
#vote = current_user.votes.build(value: new_value, post: #post)
authorize #vote, :create?
#vote.save
end
end
private
def load_post_and_vote
#post = Post.find(params[:post_id])
#vote = #post.votes.where(user_id: current_user.id).first
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :questions
resources :advertisements
resources :topics do
resources :posts, except: [:index] do
resources :summaries, only: [:create, :new, :show]
resources :comments, only: [:create, :destroy]
post '/up-vote', to: 'votes#up_vote', as: :up_vote
post '/down-vote', to: 'votes#down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
get 'contact' => 'welcome#contact'
root to: 'welcome#index'
end
Module providing methods within specs:
module TestFactories
def associated_post(options={})
post_options = {
title: 'Post title',
body: 'Post bodies must be pretty long.',
topic: Topic.create(name: 'Topic name'),
user: authenticated_user
}.merge(options)
Post.create(post_options)
end
def authenticated_user(options={})
user_options = {email: "email#{rand}#fake.com", password: 'password'}.merge(options)
user = User.new(user_options)
user.skip_confirmation!
user.save
user
end
end
Relevant Rake Routes
topic_post_up_vote POST /topics/:topic_id/posts/:post_id/up-vote(.:format) votes#up_vote
topic_post_down_vote POST /topics/:topic_id/posts/:post_id/down-vote(.:format) votes#down_vote
Any ideas why I'm getting this URL generation error?
I think you just need to pass the topic_id to the post method like this: post( :up_vote, post_id: #post.id, topic_id: #post.topic.id )
I ended up switching my routes.rb file to the below and got the above vote_controller_spec.rb working.
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :questions
resources :advertisements
resources :topics do
resources :posts, except: [:index]
end
resources :posts, only: [] do
resources :summaries, only: [:create, :new, :show]
resources :comments, only: [:create, :destroy]
post '/up-vote', to: 'votes#up_vote', as: :up_vote
post '/down-vote', to: 'votes#down_vote', as: :down_vote
end
get 'about' => 'welcome#about'
get 'contact' => 'welcome#contact'
root to: 'welcome#index'
end