How to enable registration - ruby-on-rails

I have a website that works without problems. Here is the routes file:
Testpager::Application.routes.draw do
devise_for :users
resources :news, only: [:index, :show]
resources :documents, only: [:index, :show]
namespace :admin do
resources :documents, only: [:index, :show, :destroy, :edit, :update, :new, :create]
resources :news, only: [:index, :show, :destroy, :edit, :update, :new, :create]
get "admin" => 'documents#index'
end
get "contacts/index"
get "services/index"
get "index/index"
get "admin/index"
root 'index#index'
end
I would like to add a registration system. To do this I'm using Devise.
My actions are:
I installed the Devise gem.
Ran rails g devise:install
Added this to application.html.erb:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
Ran rails g devisw User
Ran rake db:migrate
Ran rails server
I opened the URL http://localhost:3000/users/sign_up
I got the following error message:
ActionController::UrlGenerationError in Devise::Registrations#new
Showing /home/kalinin/rails/testpager/app/views/shared/_nav_main.html.erb where line #6 raised:
No route matches {:action=>"index", :controller=>"devise/index"}
Extracted source (around line #6):
<ul>
<li>
<%= link_to 'index', controller: "index", action: "index" %>
</li>
Trace of template inclusion: app/views/layouts/application.html.erb
Here is the controller:
class IndexController < ApplicationController
def index
end
end
The view for IndexController exists.

Check out this link About Adding Custom fields to a Devise registration: http://jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html. What you will need to do is set up a Registrations Controller that inherits from Devise::RegistrationsController. This quick tutorial is easy to read and understand.

Related

No route matches for devise sessions destroy path

I have installed devise to my rails app and was originally able to sign out using <%= link_to('Logout', destroy_user_session_path) %>
However, somewhere along the line after adding more code and a few gems (paperclip, act_as_votable, social_share_button) I was not able to use the same link. When I click the link_to I receive the error
No route matches [GET] "/users/sign_out"
I have also tried adding config.sign_out_via = :get to my devise.rb file but still got the same error. What have I done wrong?
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' }
root to:'ideas#index'
get "/page", to: 'pages#index'
resources :ideas, only: [:index, :show, :create, :destroy, :new] do
member do
put "like", to: "ideas#upvote"
put "dislike", to: "ideas#downvote"
end
end
resources :comments, only: [:create]
end
sessions controller
class Users::SessionsController < Devise::SessionsController
def new
super
end
def create
super
end
def destroy
super
end
end
I believe <%= link_to 'Logout', destroy_user_session_path, method: :delete %> is what you're looking for

Clearance gem redirect issues

Hello I'am using clearance gem for user authentication.
So far everything works perfect, but I'am confused why even after setting the root routes for my pages index view, it still redirects me to the sign_in page of clearance gem.
I have looked at github documentation notes, and it list no other work around for this. I'am doing something wrong?
Here is what my routes.rb file looks like:
Rails.application.routes.draw do
resources :passwords, controller: "clearance/passwords", only:
[:create, :new]
resource :session, controller: "clearance/sessions", only: [:create]
resources :users, controller: "clearance/users", only: [:create] do
resource :password,
controller: "clearance/passwords",
only: [:create, :edit, :update]
end
get "/sign_in" => "clearance/sessions#new", as: "sign_in"
delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
get "/sign_up" => "clearance/users#new", as: "sign_up"
root 'pages#index'
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
end
the clearance require_login before_action must be active for that route. Did you add that before action to application controller? Does PagesController inherit from ApplicationCobtroller? If so, you need to add skip_before_action :require_login to your pages controller. Perhaps scoped to the particular pages controller action you want unprotected.

Delete/Destroy is not working within Rails

I am trying to get a delete button to work using a piece of code I have written within my tags_controller page;
def destroy
#tag = Tag.find(params[:id])
#tag.destroy
redirect_to :back, notice: 'Tag was successfully deleted!'
end
When I run from my local host, it throws an exception as shown below;
Routing Error
No route matches [DELETE] "/admin/tags/37/edit"
Rails.root: /Users/laurenwoodhams/Desktop/PROJECT/RAILS-BLOG/-t
Here is my config routes;
Rails.application.routes.draw do
get '/login' => 'admin/sessions#new'
get '/logout' => 'admin/sessions#destroy'
namespace :admin do
resources :posts
resources :tags, except: [:index]
resources :sessions, only: [:new, :create, :destroy]
resources :administrators, only: [:index, :edit, :update]
end
end
Could you please examine and explain why this may be occurring?
Thank you
DELETE is the HTTP verb (like GET or POST) which means..
To make a link to delete your tag you'll need to do something like this in your view.
<%= link_to "Delete Tag", #tag, method: "delete" %>
Then it should work properly.
P.S.
You can run bin/rake routes to see your routes.

Rails routes: GET method redirecting to show method

I have simple controller and routes file.
In my route and controller i have created a module. I wrote a simple method which is redirecting me show. I am not sure why.
Controller
module Seller
class CampaignsController < Seller::BaseController
before_action :confirm_logged_in
def viewAllCampaigns
#campaigns = Campaign.all
end
def show
end
end
end
Routes file
scope module: 'seller' do
#namespace :power do
resources :dashboard, only: [:index]
resources :sessions, only: [:create, :destroy]
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
get 'viewAllCampaigns' => 'campaigns#viewAllCampaigns'
end
Output
Started GET "/campaigns/viewAllCampaigns" for 127.0.0.1 at 2015-10-12 17:39:43 +0500
Processing by Seller::CampaignsController#show as HTML
Parameters: {"id"=>"viewAllCampaigns"}
Rendered seller/campaigns/show.html.erb (0.1ms)
I am hitting http://localhost:3000/campaigns/viewAllCampaigns in browser.
Ideally your routes should be defined like this.
resources :campaigns, only: [:index, :create, :show, :update, :destroy] do
get 'viewAllCampaigns', on: :collection
end
The first comment on the routes.rb file is The priority is based upon order of creation: first created -> highest priority. This is the reason your route is redirecting to show. Rails is treating this url as campain/:id.
Routes are tested in order, from top to bottom. The show route you've added for the campaigns resource will look for urls matching this pattern:
/campaigns/:id
/campaigns/viewAllCampaigns matches this, so it will do the show action., with params[:id] = "viewAllCampaigns"
Move the special case route up above the resources#campaigns route to fix this, then it will catch the url first.
get 'viewAllCampaigns' => 'campaigns#viewAllCampaigns'
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
It takes the following get request as a show action because show expects campaigns/:id, and it assumes 'viewAllCampaigns' is an id in this instance:
/campaigns/viewAllCampaigns
Your link_to should just be pointing to the following:
'/viewAllCampaigns'
Your route structure isn't really RESTful, but that's a separate topic.

Multiple rails resource routes split across scope

Im having some issues trying to split up a controller based on scope.
My code looks like this
scope '/admin' do
resources :pages, only: [:index, :create, :new, :edit, :update, :destroy]
end
resources :pages, only: [:show]
Ideally, Id just like to have the show page not behind the admin scope, however when I call page_path(1), it routes to /admin/pages/1, not /pages/1.
What is the easiest way to work around this issue?
Thanks
Updated with routes
pages_path GET /admin/pages(.:format) pages#index
POST /admin/pages(.:format) pages#create
new_page_path GET /admin/pages/new(.:format) pages#new
edit_page_path GET /admin/pages/:id/edit(.:format) pages#edit
page_path PATCH /admin/pages/:id(.:format) pages#update
PUT /admin/pages/:id(.:format) pages#update
DELETE /admin/pages/:id(.:format) pages#destroy
GET /pages/:id(.:format) pages#show
root_path GET / pages#main
And view:
<% #pages.each do |page| %>
<%= link_to page.title, page_path(page) %>
<% end %>
To influence route helper names, use as-parameter:
scope '/admin', as: 'admin' do
However, it makes sense to place the admin's controller in Admin namespace, so you can do this:
namespace 'admin' do
That will influence path (/admin), route helpers (admin_) and controller namespace (Admin::PagesController). A separate controller allows for easier access and layout control. Also, you can make a separate show for admins, which is nice to have.
Of course, in your forms you'll have to use constructs like form_for [:admin, #page], or form_for #page, url: admin_page_path(#page).
scope '/admin', as: 'admin' do
resources :pages, only: [:index, :create, :new, :edit, :update, :destroy]
end
resources :pages, only: [:show]
Just do the changes as above
scope module: '/admin' do
resources :pages, only: [:index, :create, :new, :edit, :update, :destroy]
end

Resources