link_to "about",about_path showing the error in ROR - ruby-on-rails

i am working in Ruby on rails.. i am new to this..
i have used a line
<%= link_to "about",about_path %>
which throws me a error as,
undefined local variable or method `about_path' for #<ActionView::Base:0xb5f5baa8>
i am having the page about.html under app/views/pages/
please give some suggestions of why i am getting like this .

I just had the same problem using the Hartl's tutorial. Here is what I did.
When asking rake routes, I have :
tomsihap-MBP:sample_app tomsihap$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact
Then the correct path is :
<%= link_to "About", static_pages_about_path %>
And not <%= link_to "About", about_path %> as suggested by Hartl's guide.
EDIT :
Ok now I understand. That is because routes were defined like that :
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Instead of, later explained into the tutorial :
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
Using this way, the correct path now is :
<%= link_to "About", about_path %>

Your code is looking for what is called a named route. You need to define these in config/routes.rb. In addition you will need some controller and action to handle them. See this post describing a very simple way to handle static pages by way of illustration.
To get the about_path named route, then you would add this to routes.rb
map.about "/pages/about", :controller => "pages", :action => "show", :id => "about"
Then add your about page contents to a file called app/views/pages/about.html.erb
Finally:
$ rake routes
tells you all of the named routes defined for your application and what they do

I guess your about page is "static". Check this..
routes.rb
# rails 2.3.x
map.about "/pages", :controller => 'pages', :action => 'about'
Controllers/pages_controller.rb
class PagesController < ApplicationController
def about # not needed, only for "tidiness"
end
end
... and your erb file have to be here: Views\pages\about.html.erb

Is in your routes.rb something like map.resources :about ?
If you don't know why it should be there or what is that, read about RESTful Routing on guides.

Related

Routing Error No route matches [GET] "/about_path"

I am beginner to ror, I am trying dynamic page in my application, while I click the about link in my application I got a routing error(as said in title). Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
resources :userdetails do
collection {post :import}
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"
get 'about', to:'userdetails#about'
devise_scope :users do
get 'sign_in', to: 'devise/sessions#new'
get 'sign_out', to: 'devise/sessions#destroy'
end
end
here is index.html.erb file:
<div class="topnav">
Home
News
Contact
<a href="about_path" data-method = "get" >About Us</a>
...
</div>
Please help me identify my mistake.....
your routes says: -
get '/about', to: 'userdetails#about', as: :user_about
so according to this
About Us
Instead of this you can use rails view helper which fill convert this to html anchor tag: -
<%= link_to 'About Us', user_about_path%>
As you are beginner so you should go though this rails Routing for better clarification.
Note: - by default link is GET type http verb so you don't need to mention data-method = "get"
and you can see all your routes at console by running command rake routes
When you run rails routes or rake routes it will generate like this
about GET /about(.:format) userdetails#about
That means your path is about_path then you can call out like this
About Us
or like this HTML
About Us
or like this .erb
<%= link_to 'About Us', about_path %>
While your routes like exist
get 'about', to:'userdetails#about'
Here you have defined method get that's why this is unnecessary to HTML call out data-method = "get".
Hope it will work

I am working on An Ruby on Rails app

I am working an an Rails app. The problem I am have is when I cycle between my about and contact pages. I always get the error
No route matches [GET] "/pages/pages/about"
or
No route matches [GET] "/pages/pages/contact"
I'm trying to change the routes my nav bar partial the tag href to "/about" but the same error occur. it ask me to use the command rake routes and it shows
$ rake routes
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
pages_about GET /pages/about(.:format) pages#about
root / restaurants#index
pages_contact GET /pages/contact(.:format) pages#contact"
can some one please help me!!
Your about and contact are under the url /pages/about and /pages/contact but the URL you are accessing is /pages/pages/about which doesn't exist.
You can see in the rake routes, the possible urls in the system.
you need to have the link in your web app as
<%= link_to 'About', pages_about_path %>
<%= link_to 'Contact', pages_contact_path %>
in your routes you can do something like
#config/routes.rb
ClientCruiser::Application.routes.draw do
....
match "contact" => "pages#contact", :as => :contact, via: :all
match "about" => "pages#about", :as => :about, via: :all
....
root :to => 'pages#index'
end
The output of this
contact /contact(.:format) pages#contact
about /about(.:format) pages#about
root GET / pages#index
the way you would make the call would be
<%= link_to about_path, "About", class: '' %>
<%= link_to contact_path, "Contact", class: '' %>

How to remove /posts/ from URL?

How can I also show a blog post without /posts/ in the URL?
http://www.anthonygalli.com/posts/i-walk-the-line
http://www.anthonygalli.com/posts/50-shades-of-galli
http://www.anthonygalli.com/posts/tips-for-regaining-momentum
Shortened Version (Goal of Question):
http://www.anthonygalli.com/i-walk-the-line
http://www.anthonygalli.com/50-shades-of-galli
http://www.anthonygalli.com/tips-for-regaining-momentum
I have post as a MVC.
routes.rb
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
Change your routes.rb to:
resources :posts, path: '/' do
collection do
get 'about'
get 'feed'
get 'subscribe'
end
end
For documentation on Rails routing check: http://guides.rubyonrails.org/routing.html
Your routes should be updated to look something like this:
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
get ':id' => 'posts#show', :as => :post_by_slug
That as option will be important for linking to your new route if you want to link to the /post/-less paths:
<%= link_to post.title, post_by_slug_path(post) %>
P. S. I believe that the new route will need to be listed last in your routes so that the routes before it can have precedence. (Otherwise, trying to visit /about would try to load a post with a slug named about instead of the posts#about action. You should try this and see if that's true.)
Resource generates the standard CRUD urls, so you have to specify the posts route specifically. Something like this
get '/desired-url' => 'posts#index'

Couldn't find User with id=sign_out

Sign-out link isn't working in my rails application.
I have checked my routes.rb which is listed below and my application.html.erb looks to follow the right path.
Getting the following error.
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Rails.root: /Users/patrickwalsh/rails_projects/ytutorial
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:4:in `show'
lib/disable_assets_logger.rb:11:in `call'
My routes.rb
Refectory::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "users" }
devise_scope :user do
get 'login', to: "devise/sessions#new", as: "login"
get 'logout', to: "devise/sessions#destroy", as: "logout"
get 'logout', to: "users/sessions#destroy", as: "logout"
get 'signup', to: "users#new", as: "signup"
match '/users/:id', :to => 'users#show', :as => :user
end
root :to => 'tutorials#index'
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
get 'users/:id' => 'users#show'
end
resources :tutorials
resources :comments, only: [:show, :create, :update, :destroy]
resources :tutorials do
member { post :vote }
end
if Rails.env == "development"
match 'errors/404' => 'errors#error_404'
match 'errors/500' => 'errors#error_500'
end
unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end
match 'tagged' => 'tutorials#tagged', :as => 'tagged'
end
and my application.html which seems to be following the right route from what I can see.
Any help is greatly appreciated!
<% if current_user.present? %>
<li><%= link_to "Log out", destroy_user_session_path, (:method => "delete") %></li>
<% else %>
<li><%= link_to "Log in", new_user_session_path %></li>
<li><%= link_to "Sign up", new_user_registration_path %></li>
<% end %>
My users controller as well as I have a suspicion this is where the problem lies but not sure what the error is.
class UsersController < Devise::RegistrationsController
def show
#user = User.find(params[:id])
#tutorials = #user.tutorials
end
end
I had the same issue. My routes were in the correct order, the link_to method was properly used, and Rails kept triggering the users/:id route with :id => :sign_out. I figured out that was because I removed jquery_ujs from my application.js file...
jquery_ujs handles the data-method attribute in the links (generated by link_to when you use the method option), which is used to determine the correct route as explained here: https://thoughtbot.com/blog/a-tour-of-rails-jquery-ujs
So just make sure the you have the following included in your application.js:
//= require jquery
//= require jquery_ujs
If you are calling /users/sign_out directly from the URL it won't work because the routes is:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
id est, it uses the DELETE method. You need to change your devise initializer to:
config.sign_out_via = :get
other option would be to create a small form and its button with DELETE as method.
This worked for me
#form
<%= link_to(destroy_user_session_path, {:class => "nav-link", :method => :delete}) do %>
<span class="sidebar-normal"> Logout </span>
<% end %>
#routes
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
I have started noticing this error after removing rails-ujs. This was done as part of the upgrade to Rails 7 with ImportMap and Hotwire suite. Changing link_to to button_to has fixed this error in this case.
<%= button_to 'Log out', destroy_user_session_path, method: :delete %>
https://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#replacements-for-rails-ujs-functionality
None of this solutions worked for me.
Also it happens just in development mode for me...
I fixed it by adding
if params[:id] = "sign_out"
sign_out current_user
return
end
in the set user function. Not the prettiest solution but it works...
You need to move:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
over your devise_scope. Rails is looking for routes from top of Routes file. Your sign out url matches users/:id, hence it is trying to render show action with sign_out being an id.
UPDATE:
Actually, do you really need the last line in your devise_scope block?
Since non of the other answers worked, I found that you could change the base path for every Devise endpoint as described here. So, what I did was to user devise_for on routes.rb:
devise_for :users,
path: 'devise'
Then, all my Devise routes started with devise instead of users so the conflict was gone.
Sorry to bump this up but the "correct" anwser is not to mess around with routes risking breaking many things.
IF you want a disconnect on a GET then actually configure Devise like so in initializer/devise.rb
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
As mentionned in the Devise documentation.
Short answer: use link_to with method DELETE
<%= link_to 'Logout', destroy_user_session_path(current_user), method: :delete %>
explanation:
If you take a look at your Routes you'll see
Helper
destroy_user_session_path
the HTTP verb is
DELETE
the Path
/users/sign_out(.:format)
Controller#Action
devise/sessions#destroy
link_to defaoult method is get, this is "the why" (in a simplistic way of explaning). However, you can specify by using method: :method name
I got this error when using devise in a rails 7 app (with bootstrap):
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=sign_out)
I don't fully understand it myself yet, but basically just open another terminal window, type ./bin/dev and hit enter, and (I guess) it loads your javascripts.

Ruby on Rails - Route to home#index

This one is a two-parter. Initially I had just one controller:
$ rails generate controller home index
...and I made that my root.
Recently, though, I wanted to get the database involved. So I made a scaffold called Daily and switched the root to dailies#index instead of home#index.
That worked just fine. Everything seems to be working fine.
...only problem is that I want to use home#index for something else... but now I have no way to link to it.
This is my routes.rb file:
LiquidAdmin::Application.routes.draw do
resources :dailies
devise_for :users
get '/auth/:provider/callback', to: 'sessions#create'
get "home/bandwall"
get "home/index" :to => "home#index"
# get "dailies/index"
root :to => "dailies#index"
and these are my routes:
home_index GET /home/index(.:format) home#index
I've tried.
<% link_to "Home", home_path %>
<% link_to "Home", home_index %>
<% link_to "Home", home/index %>
Nothing will get me back to that file... even though it was working just fine when it was the root.
Now all of this could've been avoided if "Plan A" had worked...
Plan A was just to make this Daily scaffold and do something like:
<% #dailies.each do |daily| %>
This worked as a link from the Daily index... but not as a link from home#index. Which is why I switched the root. I was getting annoyed and it was easier.
So my questions: 1. How do I link to the old home#index? 2. How should I have queried the dailies tables from home#index?
Assuming you have a Daily model.
routes.rb
resources :dailies
get "home/index"
Running rake routes:
dailies GET /dailies(.:format) dailies#index
POST /dailies(.:format) dailies#create
new_daily GET /dailies/new(.:format) dailies#new
edit_daily GET /dailies/:id/edit(.:format) dailies#edit
daily GET /dailies/:id(.:format) dailies#show
PUT /dailies/:id(.:format) dailies#update
DELETE /dailies/:id(.:format) dailies#destroy
home_index GET /home/index(.:format) home#index
app/controllers/dailies_controller.rb
class DailiesController
def index
#dailies = Daily.all
end
end
app/views/dailies/index.html.erb
<%= link_to "Home", home_index_path %>
<% #dailies.each do |daily| %>
<%= link_to 'A daily', daily_path(daily) %>
<% end %>

Resources