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 %>
Related
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>
I am new to rails and trying to figure out how to pass id from view to controller. I have create below routes.rb file i didnt want resources here just to have better understanding of how to pass params
Rails.application.routes.draw do
get 'sites/edit/:id', to: 'sites#edit'
get 'sites/main'
devise_for :users
root 'sites#main'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
below is my controller
class SitesController < ApplicationController
def main
#site = Site.all
end
def edit
#site = Site.find(params[:id])
end
private
def site_params
params.require(:site).permit(:title, :subtitle,:name)
end
end
and i have two views for my site controller. In main.html.erb
<h1>Sites#main</h1>
<p><%= current_user.email %> user is signned in </p>
<%= #site.each do |temp| %>
<p><%= temp.name %></p>
<p></p>
<p></p>
<%= link_to "Edit Site", sites_edit_path(#temp) %>
<% end %>
I am not able to link it to correct controller.
First: your edit routes should not be
get 'sites/edit/:id', to: 'sites#edit'
Instead it should be get 'sites/:id/edit' => 'sites#edit'
Second: your link to method should not be
<%= link_to "Edit Site", sites_edit_path(#temp) %>
instead it should be <%= link_to "Edit Site", sites_edit_path(temp) %>
You can get to know about your routes from terminal by doing rake routes or If you want a browser view then
http://localhost:3000/rails/info/routes
You need to replace
<%= link_to "Edit Site", sites_edit_path(#temp) %>
to
<%= link_to "Edit Site", sites_edit_path(temp) %>
You have the variable temp not #temp
Also, As sarcastic suggested you need to change the route entry to
get 'sites/edit/:id', to: 'sites#edit', as: :sites_edit
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.
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?
I have a very basic sign up and log in setup running and all I want to know if how to add a link at the very top of my root page that displays 'Log in' or 'Sign out' depending on whether the user is logged in or not.
I have tried various methods I have found on here but can't seem to get them to work as they often create undefined method errors.
What is the simplest way to create this?
Many thanks in advance for your help.
Tom
if you have a session variable where you save the id of the current user (i call it user_id) you could do it like this:
<% if session[:user_id] %>
<!-- user is logged in -->
<%= link_to logout_path %>
<% else %>
<!-- user is not logged in -->
<%= link_to login_path %>
<% end %>
that is what you have to change:
config/routes.rb:
resources :users
# login stuff
controller :sessions do
get "login" => "sessions#new"
post "login" => "sessions#create"
delete "logout" => "sessions#destroy"
end
app/views/sessions/new.html.erb:
# replace this line
<%= form_tag new_session_path do %>
# with
<%= form_tag login_path do %>
the login link is now:
<%= link_to "Login", login_path %>
the logout link:
<%= link_to "Logout", logout_path, :method => :delete %>
Not much of an answer but this Railscast was very helpful to me in learning about how authentication works in rails. The Railscast is Twitter login specific using OmniAuth but the process is much the same. He includes the dynamic links you asked about in his code.
http://railscasts.com/episodes/241-simple-omniauth