Error while trying to delete a session in Rails - ruby

New to Rails. I have a model named Admin in my rails application. when i tried to delete the session i am getting No route Match Error.
I have created session controller to manage sessions shown below
class AdminSessionsController < ApplicationController
before_action :confirm_logged_in, :except => [:new, :create, :destroy]
def destroy
session[:admin_id] = nil
flash[:notice] = "You are now logged out"
render("new")
end
end
Routes I defined in routes.rb
resources :admin_sessions, only: [:new, :create, :destroy]
I have link in some other view to delete a session
<%= link_to "Sign out", :controller => "admin_sessions", :action => "destroy" %>
And I am getting the following Error
No route matches {:action=>"destroy", :controller=>"admin_sessions"}
Please help somebody!

You define resources while it's resource. You do not depend on specific id, so it always should respond with one resource (DELETE /admin_sessions/id vs DELETE /admin_sessions). Change in your route resources to resource and it will work.

Related

Routing Issue in Rails 4

I have created a form useing form_for -
= form_for #category, url: url_for(:controller => 'admin/category',:action => new_record ? "create" : "update"), name: 'udfFieldForm', id: 'udfFieldForm',:method =>'POST', remote: true do |f|
controller is look like-
class Admin::CategoryController < ApplicationController
def create
end
def update
end
end
route defined as -
namespace :admin do
get 'category/:action' => 'category#index', :as => :category
resource :categories
end
When i have submit form it through an error like -
AbstractController::ActionNotFound (The action 'category' could not be found for AdminController):
Here category is a controller under admin directory but it's looking for category action in admin controller. here i want to call category controler.
Please help me , where is the issue?
The application expects a controller named AdminController with a method named category, but it cannot find it. That is what the error message says and this how you created the link ...url: url_for(:controller => 'admin/category'...
I think that is wrong, since obviously you don't have a category method/action in your AdminController.
Need To Update Routes Like as with in admin namesapce
post 'category/:action'=> 'category', action: :create
patch 'category/:action'=> 'category', action: :update

Working with nested resources

Imagine this. I'm on the URL http://localhost:3000/companies/3, and on this view, I want an employees button that takes me to http://localhost:3000/companies/3/employees
In my routes file, I have
namespace :companies, :only => [:index, :show, :new, :edit, :destroy], :path => "/:companies" do
resources :employees, :only => [:index, :edit, :new, :show] do
end
end
I understand that I need to link to the index action of 'employees', but I don't know what to add to my companies 'show' page. Right now, it looks like
%a{title: "company1", href: "#"} Employees
(I am using haml, but feel free to post your answer in erb)
In rake routes, I get
companies_employees GET /:companies/employees(.:format) companies/employees#index
... but I want companies/company_id/employees
Thanks!
link to employees in the company show page
<%= link_to 'Employees', company_employees_path(#company) %>
employees controller
def index
#company = Company.find(params[:company_id])
#employees = #company.employees
end
routes
resources :companies do
resources :employees
end
rake routes
company_employees GET /companies/:company_id/employees(.:format) employees#index

How to setup a rails 3 route for a nested resource and custom controller action

I have a recommendation that is nested below Categories and Awards.
So Category/:id/awards/:id/recommendations/:id
I have an Assets model that handles paperclip attachments to the Recommendation. A Recommendation has_many Assets, Assets belong_to :recommendation
In my Recommendation new/edit views I am rendering a form partial (as is normal) that gives the user the option to upload several assets.
If there are Assets already related, then it lists them. I am working on setting up a custom delete action on the recommendations controller.
My current link_to:
<%= link_to "Delete Attachment",
{:controller => :recommendations, :action => :destroy_asset, :id => asset.id },
{:remote => "true", :confirm => "Are you sure you want to delete this image?"}
%>
My Controller action:
def destroy_asset
##recommendation = Recommendation.find(params[:id])
#asset = Asset.find(params[:id])
#asset.destroy
respond_to do |format|
format.js
end
end
routes:
resources :recommendations
resources :categories do
resources :awards do
resources :recommendations
end
end
I am still learning remote => true, and how to route this sucker. Not sure if I need to have the route nested or not. I tend to think not. Since I have an Asset.id in my loop, I should just be able to execute the destroy without needing the Recommendation at all.
So the question/s: do I need a route to access a custom action in my recommendations_controller?
thanks
Try this:
resources :categories do
resources :awards do
resources :recommendations
member do
get :destroy_asset
end
end
end
end
Or If you only want only destroy_asset action under recommendation controller, do something like this :
resources :recommendations
member do
get :destroy_asset
end
end
Or If you want custom match, do something like this :
match "asset/:id/destroy_asset", :to => "recommendations#destroy_asset",
:as=> "destroy_asset"

Rails finds no route in a on-to-one relation (using Devise)

I'm facing a problem with a nested route in Rails an I can't figure out, what I'm doing wrong.
In a nutshell: I use Devise for authentication an registration. Now I want to have the user enter more detailed information about his contact data. To keep the user model small, I want to use a different model called account. As a user only will have one account, I use a on-to-one association and a nested route. But somehow the routing does not work.
This is my user model:
user.rb
class User < ActiveRecord::Base
has_one :account, :dependent => :destroy
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
and this is my account model:
account.rb
class Account < ActiveRecord::Base
attr_accessible :anrede, :land, :nachname, :plz, :stadt, :strasse, :user_id, :vorname
belongs_to :user
end
and my route file looks like this:
routes.rb
devise_for :users, :path => 'members'
resources :users do
resource :account
end
As a user might not have a account yet, I test this in my view:
<% if current_user.account.try %>
<li><%= link_to "Account", user_account_path %></li>
<% else %>
<li><%= link_to "create Account", new_user_account_path %></li>
<% end %>
but when I enter the root path with a signed in user, Rails tells me
Routing Error
No route matches {:action=>"new", :controller=>"accounts"}
but there is a new action in my accounts_controller.rb as I have scaffolded the whole CRUD set (edited create with current_user.build_account) and it's also the path given by rake routes.
I'm desperately stuck in this! Could anybody help me, please?
EDIT
This is the output of my rake routes:
user_account POST /users/:user_id/account(.:format) accounts#create
new_user_account GET /users/:user_id/account/new(.:format) accounts#new
edit_user_account GET /users/:user_id/account/edit(.:format) accounts#edit
GET /users/:user_id/account(.:format) accounts#show
PUT /users/:user_id/account(.:format) accounts#update
DELETE /users/:user_id/account(.:format) accounts#destroy
EDIT2
this is the error message for the action new form:
NoMethodError in Accounts#new
Showing /home/stonjarks/Work/toytrade_devise/app/views/accounts/_form.html.erb where line #1 raised:
undefined method `accounts_path' for #<#<Class:0xa44d0cc>:0xab6222c>
Extracted source (around line #1):
1: <%= form_for(#account) do |f| %>
2: <% if #account.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#account.errors.count, "error") %> prohibited this account from being saved:</h2>
I solved this using this SO hack, but it's a strange behavior anyway:
Rails Nested Route For Singular Resource
<%= form_for #account,:url=>{:action=>:create}
But I still don't get the point of this routing anyway. Despite this, I can't manage to find a route to show the account:
/users/1/account
ActiveRecord::RecordNotFound in AccountsController#show
Couldn't find Account without an ID
new_user_account_path is missing a user instance which should be provided for example:
new_user_account_path(current_user)
If you look at the following line:
new_user_account GET /users/:user_id/account/new(.:format) accounts#new
You can see that the path demands a :user_id - a user instance.
You can read here for further clarification of your problem.

Form behavior posting nested resource

I am using Ruby on Rails 3 and I would like to change its conventional behavior on posting a form in order to post from a signup action to the create action instead that from the new action. That is, I would like to use the signup action instead of the (conventional) new action in my User controller and trigger the create action to save my model data that contains nested resources.
In my /config/routes.rb file I have:
resources :users do
collection do
get 'signup'
end
resource :profile
end
In my /app/controllers/users_controller.rb I have
class UsersController < ApplicationController
def signup
#signup_user = User.new(params[:user])
#signup_user.build_profile # NOTE: Nested resource
...
end
def create
...
#signup_user.save
respond_to do |format|
format.html { render :action => :signup } # signup.html.erb
end
end
end
In my /app/views/users/signup.html.erb file I have
<%= form_for #signup_user do |f| %>
...
<% end %>
My problem is that if I submit the above form, I will be redirected to the index action of the user controller and not to the create action I expect. It seams that the form posts only to the index action.
How can I solve the problem?
I tryed to use the following
<%= form_for( :user, #signup_user, :url => { :controller => "users", :action => "create" }, :html => { :method => :post } do |f| %>
but I have still the problem: I am redirected to the index action.
SOLUTION
The problem did seam to be in the routers.rb. The correct code was
resources :users do
collection do
get 'signup'
post 'create'
end
resource :profile
end

Resources