No route matches [GET] "/users/country.html" - ruby-on-rails

It is nice when I directly open 'http://localhost:3000/country.html' or click 'Country' button on navigation bar. But if I click 'Login'/'Signup'(http://localhost:3000/users/signin.html) first and then click 'Country' button, the url link to 'http://localhost:3000/users/country.html'. It shows 'No Route matches [GET] "/users/maison.html"'. Even more, the colour of 'Login' changes to a strange red which is similar to the default red '404' page in public.
Could anybody tell me why different clicking sequences will lead to different urls and how can I figure out the issue? Thanks!
This is an app based on Ruby on Rail, with devise, ruby-2.3.7,'rails', '~> 5.2.2' and mysql. I am using macOS Mojave.
'''
'application.html.erb':
<div class="outside_container">
<div class="main_container">
<div class="navbar clearfix">
<div class="container">
<ul class="nav left">
<li>Introduction</li>
<li>Country</li>
<li>Maison</li>
</ul>
<% if notice %>
<p class="notification"><%= notice %></p>
<% end %>
<% if alert %>
<p class="notification"><%= alert %></p>
<% end %>
<ul>
<p class="nav right">
<% if user_signed_in? %>
<%= 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 %>
</p>
</ul>
</div>
</div>
'''
sorry for missing route.rb, here it is:
'route.rb':
Rails.application.routes.draw do
devise_for :users
#make sure devise/session/new, which means the login page is the root page
devise_scope :user do
authenticated :user do
root 'country#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
get '/country', to: 'country#index'
get '/maison', to: 'maison#index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
#root 'page#welcome'
resources :country
resources :maison
resources :introduction
resources :dashboard
end

So it looks like you have an href <li>Maison</li> that is pointing to /maison which means somewhere in your routes.rb there needs to be a route defined that matches that, as well as a controller that the route interfaces with:
get '/maison', to: 'home#index`
In the above route, we're saying "any get request to the /maison address should trigger the index method in the home_controller file in Rails.
Once you set the route and controller correctly, this should work properly.

There are a couple things:
The file should be titled routes.rb (plural)
So change route.rb to routes.rb.
Use the command $rails routes or $rake routes
To get a list of all routes you currently have available to you. If nothing shows, then rails is not recognizing your routes.
Use "Link To" for example
<%= link_to user.title, users_path(user) %>

Remove the dot from your URLs. The dot makes it a relative path, so wherever you are in your app, it will use that path as the starting point for those links. If you remove the period, it will always use your root path to build the links.
<li>Introduction</li>
<li>Country</li>
<li>Maison</li>

Related

How can I connect my home page with other pages?

I am using the Gem 'Devise' in order to set users and admins for my page. I have created a log in page which requires the user to first sign in and then after the succesful log in I want to redirect the user to the home page of the Model Category, which is gonna be displaying just a list of categories of products. Yet, I have not found the proper way to do it. This is my routes file:
Rails.application.routes.draw do
resources :categories
root to: 'pages#home'
devise_for :users
end
And this is my home.html.erb file inside the view pages :
<h1>Login page</h1>
<% if current_user %>
<%= link_to 'Categories', category_path(#category) %>
<%= link_to 'Sign Out', destroy_user_session_path, method:
:delete %>
<% else %>
<%= link_to 'Sign Up', new_user_registration_path %>
<%= link_to 'Sign In', new_user_session_path %>
<% end %>
I get an error saying:
ActionView::Template::Error
(No route matches {:action=>"show",:controller=>"categories", :id=>nil},
missing required keys: [:id]):
How can I connect the home.html.erb file of the view Page with the index.html.erb of the Category view? Thanks in advance for any help provided
How can I connect the home.html.erb file of the view Page with the index.html.erb of the Category view?
If what you want is a link to view all categories, then why are you using a helper to view one singular category, category_path(#category) (and passing a non-existing #category too)? Instead, use this:
<%= link_to 'Categories', categories_path %>

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

unable to use sign_out in ruby on rails devise gem

In my small rails app I have a controller called say and two pages hello and goodbye in it. I installed devise in the app. Initially i was able to access the sing_in page as /users/sign_in and similarly for sign up. I was not able to sign out and due to not able to sign out i am not able to access sign up or sign in page again. Now I entered the following code int eh say/hello page
<h1>Say#hello</h1>
<% if user_signed_in? %>
Hey signed in as <%= current_user.email %>.Not you?
<%= link_to "Logout", destroy_user_session_path %>
<% else %>
Hey <%= link_to "Login", new_user_session_path %> or Hey <%= link_to "Logup", new_user_registration_path %>
<% end %>
</p>
This the routes page of the app.
Demo::Application.routes.draw do
devise_for :users
get "say/hello"
get "say/goodbye"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root :to => "say#hello"
end
Whenever I access the say/hello page, this is the output i get
Links
Hey signed in as x#gmail.com.Not you? Logout
When I click logout. It says
No route matches [GET] "/users/sign_out"
When I directly access users/sign_in I get routed to the say/hello page.
What mistake am I doing? How should I sign_out?
I have also tried
destroy_user_session_path, :method => :delete
Hope this could help
Try this in your routes.rb
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end

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.

Why am I seeing "No route matches :action=>"show"" when my show action is defined for that controller?

I have a "Category" resource, and am trying to link to Categories#show in its index.html.erb file using the link_to method and the _path routes. When I view the index in my browser I see the following error:
Routing Error No route matches {:action=>"show", :controller=>"categories"}
But I do have a show action defined in my Categories controller, and removing the URI part of link_to causes the index to display normally without having an exception thrown! I can't figure out where I'm going wrong with the URI.
Here's /categories/index.html.erb:
<h1>My Links</h1>
<% #categories.each do |c| %>
<h2><%= link_to "#{c.category}", category_path %></h2>
<p><%= c.description %></p>
<% end %>
<%= link_to "Add new category", new_category_path %>
Here's /categories/show.html.erb:
<h1><%= #category.category %></h1>
<p><%= #category.description %></p>
<p><%= link_to "Back to index", root_path %></p>
Here's /config/routes.rb:
LinkManager::Application.routes.draw do
resources :categories do
resources :links
end
root :to => 'categories#index'
Here's part of /controllers/categories_controller.rb:
class CategoriesController < ApplicationController
def index
respond_with(#categories = Category.all)
end
def show
#category = Category.find(params[:id])
end
And here's the result of running rake routes (put it on pastebin since I couldn't figure out how to format it nicely here): rake routes
Can anyone spot what's wrong? I've looked at many other similar routing questions here but couldn't get a solution from them. Thank you.
Try to replace:
<h2><%= link_to "#{c.category}", category_path %></h2>
with:
<h2><%= link_to "#{c.category}", category_path(c) %></h2>
See? You have to provide a category for category_path.
Or just use the magic:
<h2><%= link_to "#{c.category}", c %></h2>
And by the way, what's the purpose of interpolation here: "#{c.category}"? Wouldn't simply c.category be enough?
Well, i think if you add other route to action show it works, do this:
do this in your browser:
localhost:3000/categories/show
and in your routes.rb:
match "/categories/show/:id" => categories#show
what is the version of our ruby and the version of rails?

Resources