Devise login form in another controller - ruby-on-rails

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.

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 delete a helpers file in Rails 5?

I'm new to Rails, and after using rails g controller Users to generate a users controller, I decided to remove (i.e., rm) the generated helper in app/helpers/users.rb, because I realized I didn't need it. It seems ugly to keep a bunch of empty files around to me. This broke my app. When I try to visit any page in my, or run a test, I get this error: Couldn't find UsersHelper, expected it to be defined in helpers/users_helper.rb.
I fixed it by manually re-creating that file, but how do I get rid of it? Is that just not supported?
Edits based on questions people asked
The helper does not appear to be referenced explicitly anywhere, grep -Ri UsersHelper . only returns results for the module itself and a bootsnap cache file. The same command with :helper 'user' or 'helpers/users' returns nothing.
If Rails is telling you that it can't find UsersHelper then it means that the module is called somewhere else in the project. When the page shows you the couldn't find message, it should also indicate the offending file where UsersHelper is being included (same with the test output). If you remove this reference, you should have no problem removing the module (or directory).
I sort of figured out the issue, but I still don't understand it.
I am using some url helper functions: user_url(:id) and new_user_url, and these apparently require the helpers to exist even though they are not explicitly defined there. If anyone can explain in more depth what's going on, I'd appreciate it.

Rails controller wrong template location

I have a model, controller and routes configured sucessfully with the name animegif.
In my show method, it looks under application/show instead of views/animegif/show.
Missing template animegif/show, application/show
"searched in /Users/myName/Desktop/testapp/app/views
My easy fix was adding this method to my animegif controller but I do not understand why is it not searching under views/animegif/show by default.
When I followed the rails tutorial by Michael Hartl, the paths were located correctly. Is there something I am doing wrongly?
Name of controller: animegifs_controller, model: animegif
For my routes I am using resources to generate the default routes for the model
def self.controller_path
"animegif"
end
If you read the error message once again you can see it first animegif/show directory of your views. If rails does not find the required template in that directory then it falls back to app/views. You are only getting this error message because either the show.html.erb is not there or you have any typo error.
If you are more curious about how rails finds your views, please refer to this post by Andy Wang. He has explained it in a better way.
Regarding the error message it is looking in app/views/animegif/ and app/views/application/ but cannot find the template. So you probably have a typo in the template's name show.html.erb (or other formats than html) in the respective directory.

undefined local variable or method `sign_out_path'

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".

Rails & Refinery CMS: Determine the Controller class name from view?

I'm working with an existing Refinery CMS app for a client that has many controllers in many different places. If you are n00b to Refinery CMS, you can nest entire rails apps INSIDE the vender folder and they act like plugins. Its complex how it works and even worse a lot of the models/controllers are embedded in the refinery gem so a controller might exist but theres not file for it.
I wanted to extend a controller by following this example:
http://refinerycms.com/edge-guides/extending-controllers-and-models-with-decorators
which I did but my code was not firing. I did actually fix this so my problem is solved but in the future it would be useful to know what controller called this view I have. The view is tucked away in the gem HOWEVER a partial that it references was already overridden so I could throw something like:
<%= raise self.class.to_yaml %>
The problem with this I get the following error:
can't dump anonymous class: #<Class:0x000000061f5850>
Which isn't very helpful.
My question is this: How can I output the class name of the controller that calls any given view/partial ?
Thanks!
You can use params[:controller]
And params[:action] for current action

Resources