undefined local variable or method `sign_out_path' - ruby-on-rails

I'm beginner in rails application. I have used devise gem to authentication purpose.
when I log in its showing error:
undefined local variable or method `sign_out_path'
How can I solve this problem?

There could be two reasons for this:
You have not got the correct routes defined in your routes configuration file
You have used a path helper for an existing route but mistakenly used the wrong name
First run rake routes. Have a look through the output and see if you can see any routes beginning with "devise".
If you can see one called "destroy_user_session" then this is actually the name you need to use for your sign out link, and not "sign_out_path". In that case, go to the view where you have put your sign out link and replace the helper with "destroy_user_session_path".

Related

Getting "Uninitialized constant" routing error when loading a view

I'm a beginner to both ruby and rails, and using Rails 5.17 to develop a web app for a class.
Creating the empty Rails project was successful, but something is going wrong when creating a new controller. I generated a new controller named cars from the root of the project, which was successful. There was a file in app/controllers named cars_controller.rb which looks like this:
class CarsController < ApplicationController
end
I added a method to this file named hello that does nothing.
I then created a file named cars.html.erb in the app/views/layouts directory. This file is a basic page of html code.
In config/routes.rb, I added the following:
get '/cars', to:: 'cars_controller#hello'
resources: cars
After all of this, I ran rails server, and opened localhost:3000 in a browser. This brings up the normal Ruby on Rails welcome page.
But when I go to localhost:3000/cars, I get the following:
Routing Error
uninitialized constant CarsControllerController
I've tried changing the name of the cars_controller.rb file. I've tried changing the name of the class in the controller file from CarsController to Cars. I've tried many different routes in routes.rb. I finally tried uninstalling Rails 5.17 and installing Rails 5.13.
I'm very confused, and I'd be grateful for any advice I can get. Thanks in advance!
One of the great things about Rails is its preference for convention over configuration. However, for this to really benefit you, you need to stick to doing things “The Rails Way” rather than your own way, wherever possible.
In this case, start by getting rid of your custom get route, and just use resources :cars.
From the command line, run rake routes (you might be able to run rails routes on your rails version too) and see the routes that it has created for you.
Now, rename the method you added to your CarsController from hello to index.
Move your hello.html.erb file from app/views/layout to app/views/cars/index.html.erb.
Finally, start the rails server (rails start) and load the url http://localhost:3000/cars in your browser.
—-
Note that templates in app/views/layout have a special purpose. These are used to apply a general template to your views. Look up the use of layout within a controller for more details
I think you have an error in how you had defined your route - you don't need _controller.
Instead, try this:
get '/cars', to: 'cars#hello'
Also, keep in mind that in your cars directory you need the view: hello.html.erb

How to namespace routes in Rails 4 to set up Forem?

I've been trying to set up Forem (a Rails 4 forum engine) using a guide and original docs.
Most things work, but I'm getting route errors. In my application.erb I have this route in a link_to:
topic_path(u)
The guide recommends that I preface this with my application name so my routes won't conflict with Forem's routes, so I did that as so:
H2le.topic_path(u)
(H2le is the application name set in application.rb)
However, this errors out:
"undefined method `topic_path' for H2le:Module"
Am I not setting the application name properly?
The problem was my being a Ruby newb and the guide I was following perhaps not being super explicit. It recommended to namespace the links like:
main_app.path
And I interpreted main_app to be a placeholder for my app name. Well, wrong. main_app is a built-in helper function, so it should literally just say main_app. I fixed this, and everything worked.

Why route method not found 'change_password_admin_user' using ActiveAdmin / Devise?

Can't understand how this method isn't found?
undefined method `change_password_admin_user' for #<ActiveAdmin::Views::ActionItems:0x007ff1cad9d848>
Output of my rake routes
change_password_admin_user GET /admin/users/:id/change_password(.:format) /users#change_password
Appears that you don't have used the suffix in the method call. Two methods are generated by the route that you pasted in in your question:
change_password_admin_user_path
Which return a path relative to the domain, and the other version which includes the host of your site:
change_password_admin_user_url

Devise login form in another controller

I am trying to have the Devise log in form be on my home page.
When I just copy the form over, of course I get errors because the 'resource' variables and etc are not set in the action.
I found this solution on the internet: http://pupeno.com/blog/show-a-devise-log-in-form-in-another-page/
However, his solution is to set the needed variables in a module called ContentHelper.
Where do I put this code? I tried putting it in the initializers, but I still get the error about 'resource' variable not existing
See if this entry in the devise wiki helps you!
https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
Put the module in a file in app/helpers/content_helper.rb. If you still get errors add helper :content to your controller.

Rails: how do you access RESTful helpers?

I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8
You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.
rake routes at the command line should get you that list.
I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf
You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)
From memory, you can't call url/path helpers from the console for some reason.

Resources