Facing a NoMethodError and unable to pinpoint the source - ruby-on-rails

I added the devise gem to my rails app. Then I appended the url with user/sign_up as that is self created by the app. Then the following page shows up -
I am unable to locate the file with the following code.
This is what the bottom of the page looks like -
Do let me know what more information I can provide.
Thanks for your time and help.
EDIT 1 Added routes.rb for your reference
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts, only: :create
get 'contact-us', to: 'contacts#new', as: 'new_contact'
end
EDIT 2- Added the following piece of code to my application.html.erb file
<% if user_signed_in? %>
<%= link_to "Log Out", destroy_user_session_path, method: :delete, class: "btn btn-default navbar-btn" %>
<% else %>
<%= link_to "Log In", new_user_session_path, class: "btn btn-default navbar-btn" %>
<%= link_to "Sign Up", new_user_registration_path, class: "btn btn-default navbar-btn" %>
<% end %>
And a page displayed the error: NoMethodError in Pages#home(as in the image added) with the subsequent lines-
Showing /home/ec2-user/environment/saasapp/app/views/layouts/application.html.erb where line #31 raised:
undefined method `user_signed_in?' for #<#Class:0x000000047464c8:0x007f399e3e7ab8>
Did you mean? user_session_url

Did you follow devise docs for installing devise in rails? There is something to do with your routes.rb. Go through this article https://guides.railsgirls.com/devise and follow all the steps carefully. Also post your routes.rb maybe the problem is with your routes. Thanks, This type of question is asked here just try those step as well.
See this link as well.

Related

Why do I get Rails 7 Routing Error although route is defined?

I'm following the Rails Tutorial by Michael Hartl to build a tiny demo app. I'm stuck at the logout. This is my routes.rb:
Rails.application.routes.draw do
resources :users
get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
root 'users#index'
end
This is the relevant controller action:
def destroy
log_out
redirect_to root_url, status: :see_other
end
This is the session helper defining log_out:
def log_out
reset_session
#current_user = nil
end
and this is the link tag in the view:
<%= link_to "Log out", logout_path, data: { 'turbo-method': :delete } %></span>
Screenshot of error
When I click on the logout link, I get this error. Expected behaviour: Log user out, redirect to login screen.
What am I doing wrong?
I don't know whether it's because of Turbo, or whether Turbo is even correctly installed. I've added gem 'turbo-rails' to the Gemfile and ran bundle afterwards without any effect.
For me, adding the method: :delete worked along with data-turbo.
<span>
<%= link_to "Log out", destroy_user_session_path,method: :delete, data: { 'turbo-method': :delete } %></span>
You were accessing the DELETE method route via GET. Have you run
rails turbo:install
You may want to change the sign out via GET instead of Delete in config/devise.rb
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get # <= change this from :delete to :get and remove the `method:` in your `link_to` helper
Here's the references:
https://github.com/hotwired/turbo-rails
https://github.com/heartcombo/devise/issues/5439
Check with passing method
<%= link_to "Log out", logout_path, method: :delete, data: { 'turbo-method': :delete } %>
or
<%= link_to "Log out", logout_path, method: :delete %>
You can change link_to to button_to
<%= button_to "Log out", logout_path, method: :delete %>
It will be less magic but more reliable because not a link but form is used (in normal life, clicking on link is a GET request)

No route matches [GET] "/recipes/1/like" I want a POST request

I've never encountered this problem before. I'm getting this error.
No route matches [GET] "/recipes/1/like"
Here is my routes.rb:
Rails.application.routes.draw do
root 'pages#home'
get '/home', to: "pages#home"
resources :recipes do
member do
post 'like'
end
end
end
Here is my recipes_controller:
def like
#recipe = Recipe.create(params[:id])
Like.create(like: params[:like], chef: Chef.first, recipe: #recipe)
#flash message
flash[:success] = "Your selection was sucessful"
redirect_to :back
end
Here is my html.erb file:
<%= render 'shared/page_title', title: #recipe.name.titleize %>
<div class= "row">
<div class="col-md-4 pull-right center">
<%= gravator_for #recipe.chef, size: 200 %>
<p>
<h5>Brought to you by: <%= #recipe.chef.chefname.capitalize %></h5>
</p>
</div>
<div class= "col-xs-8 col-md-8">
<%= link_to "Edit this Recipe", edit_recipe_path(#recipe), class: "btn btn-success pull-right" %>
<h3><%= #recipe.summary.capitalize %></h3>
<div class="show_recipe">
<%= image_tag(#recipe.picture.url, size: "300x200", class: "recipe-image") if #recipe.picture? %>
</div>
<div class ="well recipe-description">
<p>
<strong> Steps:</strong>
</p>
<%= simple_format(#recipe.description) %>
<div class="pull-right">
<%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(#recipe, like: false), :method => :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i>
<% end %>
</div>
</div>
</div>
</div>
<h5><%= link_to "Return to Recipes Listings", recipes_path, class: "btn btn-warning btn-small" %></h5>
I've explicitly added the HTTP POST request to my html.erb file
%= link_to like_recipe_path(#recipe, like: true), method: :post do %>
but rails is complaining that there is no GET route request, which I never created in my routes because I need a POST request for this particular section of the web app.
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
home GET /home(.:format) pages#home
like_recipe POST /recipes/:id/like(.:format) recipes#like
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PATCH /recipes/:id(.:format) recipes#update
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
I am honestly lost. It seems that everything is in its right place.
Rails version:
Rails 4.2.5
I've defined the action, created the like model, nested the route under recipes, and explicitly requested for a post HTTP request in the html.erb page.
Any ideas would be great!
Cheers!
Here's the relevant code from my working project with a similar arrangement.
# /views/events/splits.html.erb:
<%= link_to "Add",
associate_splits_event_path(id: #event.id, split_ids: [split.id]),
:method => :put,
:class => 'btn btn-xs btn-success' %>
# routes.rb
resources :events do
member { put :associate_splits }
end
If it's helpful to see it in context, feel free to poke around the repo. Here's a link to the view: https://github.com/SplitTime/OpenSplitTime/blob/master/app/views/events/splits.html.erb
While rails link_to does allow links to make requests with a method other than GET, it is not the normal behavior of links and relies on Javascript to make a form which gets submitted behind the scenes. Rails uses jquery-ujs for this, so make sure you haven't disabled it.
That said, making POST requests is normal behavior for forms, so you could also try using button_to instead, and simply styling the button to look like a link if necessary.
<%= button_to "Like", like_recipe_path(#recipe), method: :delete, like: true %>
Additional Info:
https://github.com/rails/jquery-ujs
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
Try with
<%= link_to "LIKE", like_recipe_path(#recipe) , method: :post %>
Other option:
<%= link_to '<i class="glyphicon glyphicon-thumbs-up"></i>'.html_safe , like_recipe_path(#recipe , like: 'value' ) , method: :post %>
You need to add a GET route:
resources :recipes do
member do
get 'like'
post 'like'
end
end

Rails Forem Engine routes not working

I'm trying to mount Forem in my rails application. I added it to my Gemfile and installed it properly. However, whenever I try and load a page under Forem's control (such as "/forums"), I get NoMethodError's on the links with non-Forem routes. These links are in a partial header included in my application.html.erb, called _header.html.erb:
<div id="nav">
<div id="nav-wrapper">
<%= link_to raw("<div>Home</div>"), :root %>
<%= link_to raw("<div>Forums</div>"), :forem %>
<%= link_to raw("<div>Events</div>"), :events %>
<%= link_to raw("<div>Applications <b class='carat'>▼</b></div>"), "#"%>
<%= link_to raw("<div>Reports <b class='carat'>▼</b></div>"), "#"%>
<%= link_to raw("<div>Tutorials</div>"), "#"%>
<%= link_to raw("<div>Rules</div>"), '#'%>
<%= link_to raw("<div>Roster</div>"), :roster %>
<%= link_to raw("<div>Donations</div>"), '#'%>
<%= link_to raw("<div>Media Center <b class='carat'>▼</b></div>"), '#'%>
<%= link_to raw("<div>L.O.A</div>"), '#'%>
</div>
routes.rb:
mount Forem::Engine, :at => '/forums'
resources :events
resources :news
devise_for :users
get 'roster' => "users#roster"
get 'news' => "news#index"
get 'profile/:user' => "users#profile", :as => :user_path
root 'news#index'
The error:
NoMethodError in Forem::Forums#index
undefined method `forem_path' for #<#:0x000000031583c8>
And so on for all the other links:
NoMethodError in Forem::Forums#index
undefined method `events_path' for #<#:0x000000031583c8>
NoMethodError in Forem::Forums#index
undefined method `roster_path' for #<#:0x000000031583c8>
This has to do something with the routes.rb's scope, but I'm not sure how I can get these links working again.
I have not used Forem but I had this same issue with Monologue. I think it will help if the links in your header specify that they are part of the main_app. Example:
Original link to root path might have been:
root_path
and now should be:
main_app.root_path
The reason for this is that the Forem Rails engine is a separate Rails application and the links without the prefix only work within the current Rails app (the main app, in this case).

Strange Routing error in rails 3. Rails changes the route that is specified

I have a rails 3 app, and when I click the link to my terms page, it routes to a completely different controller, than what the routes should use. Stranger still, the route works when I'm not logged in, and I'm using devise.
I get this error when clicking the link when I'm logged in.
No route matches {:action=>"edit", :controller=>"users", :id=>nil}
<%= link_to "Terms", terms_path %>
Routes (in the order they appear in routes.rb):
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
member do
get :following, :followers
post :accept
end
end
match '/terms', to: 'static_pages#user_agreement'
Static Pages Controller
def user_agreement
end
Rake Routes
terms /terms(.:format) static_pages#user_agreement
This also happens for every other action that I've routed this way to the staticpages controller, but not for any other actions that route to different controller.
Update: Terms Page
Header:
<%= link_to "Follow", users_path %>
<%= link_to current_user.name, current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
Footer:
<%= link_to "Welcome", welcome_path %>
<%= link_to "Settings", edit_user_path(#user) %>
<%= link_to "Terms", terms_path %>
All the content is pure html.
Thanks in advance
You have a link to edit_user_path with no #user as hinted in the comments.
You should almost certainly be using current_user anyway.

No route matches {:controller=>"devise/home", :action=>"students"}

I've searched for an answer to this and nothing has helped.
I have the following in my view:
<div id="user_nav">
<% if user_signed_in? %>
Logged in as <strong><%= current_user.first_name + " " +current_user.last_name%></strong>.
<%= link_to 'Edit profile', edit_user_registration_path %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> |
<%= link_to "Login", new_user_session_path %>
<% end %>
Which is straight from the railscast on devise. Suddenly the paths are not working anymore. (sign up or sign in) for example when I click login i get the error
No route matches {:controller=>"devise/home", :action=>"students"}
Ive tried using <%= link_to "Login", :controller => '/devise/sessions', :action => 'new' %> jsut for kicks and it returns the same error.
Yes I have devise_for :users in my route file.
rake routes return all the right routes including
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
Any help would be awesome! I'm stumped!
Looking at my stack trace, something (a link) in my layout was throwing it off. I created a custom layout for devise and everything works fine now.

Resources