Ruby on rails(routing error) - ruby-on-rails

Hi Can any body resolve this error as I am getting NoMethodError for my prop_manager_controller.rb
NoMethodError in PropManagerController#new
undefined method `prop_manager?' for #<User:0x471f690>
controllr
class PropManagerController < ApplicationController
before_filter :login_required, :except => [:new, :create]
load_and_authorize_resource
# GET /prop_manager
# GET /prop_manager.json
def index
#prop_managers= PropManager.order('id:desc').page(params[:page])
respond_to do |format|
format.html #index.html.erb
format.json {render json: #prop_managers}
end
end
# GET /prop_manager
# GET /prop_manager.json
def show
#prop_managers= PropManager.order('id:desc').page(params[:page])
respond_to do |format|
format.html #show.html.erb
format.json {render json:#prop_managers}
end
end
# GET /prop_manager
#GET /prop_manager.json
def new
#prop_managers= PropManager.new
respond_to do |format|
format.html #new.html.erb
format.json {render json:#prop_managers}
end
end
# GET /prop_manager/1/edit
def edit
#prop_manager = PropManager.find(params[:id])
end
# POST /prop_managers
# POST /prop_managers.json
def create
#prop_manager = PropManager.new(params[:prop_manager])
respond_to do |format|
if #prop_manager.save
#UserMailer.delay.homeowner_welcome_email(#home_owner, params[:home_owner][:password])
UserMailer.propmanager_welcome_email(#prop_manager, params[:prop_manager][:password]).deliver
format.html { redirect_to prop_manager_path(#prop_manager), notice: 'PropManager was successfully created.' }
format.json { render json: #prop_manager, status: :created, location: #prop_manager }
else
#prop_manager.errors[:base] << #exception_message
format.html { render "new" }
format.json { render json: #prop_manager.errors, status: :unprocessable_entity }
end
end
end
# PUT /prop_manager/1
# PUT /prop_manager/1.json
def update
#prop_manager = PropManager.find(params[:id])
#prop_manager.card_validity = nil
begin
customer = Stripe::Customer.retrieve(#prop_manager.stripe_customer_id)
customer.email = params[:prop_manager][:email]
customer.card = { :number => params[:prop_manager][:credit_card_number],
:exp_month => params[:prop_manager][:credit_card_expiry_month],
:exp_year => params[:prop_manager][:credit_card_expiry_year],
:cvc => params[:prop_manager][:credit_card_cvc_code],
:name => params[:prop_manager][:credit_card_holder_name] }
customer.save
#prop_manager.card_validity = true
rescue => exception
#exception_message = exception.message
end
respond_to do |format|
if #prop_manager.update_attributes(params[:prop_manager])
format.html { redirect_to home_owner_path(#prop_manager), notice: 'PropManager account details updated successfully.' }
format.json { head :ok }
else
#prop_manager.errors[:base] << #exception_message
format.html { render action: "edit" }
format.json { render json: #prop_manager.errors, status: :unprocessable_entity }
end
end
end
# DELETE /home_owners/1
# DELETE /home_owners/1.json
def destroy
#prop_manager = PropManager.find(params[:id])
begin
customer = Stripe::Customer.retrieve(#prop_manager.stripe_customer_id)
customer.delete
rescue => exception
# Do nothing - there is no customer record in the stripe account
end
#prop_manager.destroy
respond_to do |format|
format.html { redirect_to prop_managers_url }
format.json { head :ok }
end
end
end
routes
GEMS::Application.routes.draw do
resources :customer_feedbacks
resources :general_repairs_prices
resources :steam_cleaning_prices
resources :window_cleaning_prices
resources :roof_cleaning_prices
resources :gutter_cleaning_prices
resources :residential_jobs do
member do
get 'accept'
get 'decline'
get 'print_friendly'
end
collection do
post 'create_job_with_estimate'
put 'update_multiple'
end
resources :residential_job_changes do
member do
get 'approve'
end
end
end
resources :home_owners
resources :prop_managers
resources :contractors do
member do
get 'approve'
get 'disapprove'
end
end
resources :users
resources :email_templates
resources :feedback_survey_questions
resources :decline_reasons
resources :services
resources :branches
resources :sessions
resources :password_resets
get "sites/index"
get "sites/about_us"
get "sites/home_owner"
get "sites/home_owner_front"
get "sites/prop_manager"
get "sites/owner_register"
get "login" => "sessions#new", :as => "login"
get "logout" => "sessions#destroy", :as => "logout"
end
I think I have already defined the route correctly so Please let me know any changes require.

It looks like either:
Your User model does not have the method prop_manager? on it. This is a method you'd have to add on your own -- active record on its own would not make a method available with a ? on it available.
From the look of the trace it's possible this is happening in a call back on the user model. Is there anything like that in your User model?
OR -
You're using cancan I noticed. It may be possible that the permissions of the user that's logged in do not include accessing that method -- though I'd think that cancan would provide a better error than simply saying the method is not available.

Related

Rails Dot in Url with two routes for one controller

I'm making a site with one controller "Projects" and i want to show all projects with to routes :
/admin/projects/:id = /admin/projects/1 (works)
/front/:id = /front.1 (doesn't work)
I have tried this get 'front/:id' => 'projects#show', :constraints => { :id => /[^/]+/ } in route.rb but it doesn't work.
My files :
routes.rb
Rails.application.routes.draw do
resources :users, path: '/admin/clients'
get 'admin' => 'admin#dashbord'
get 'admin/profile'
get 'admin/settings'
get 'admin/_admin_header'
get 'front' => 'front#index'
get 'front/profile' => 'front#profile'
get 'front/:id' => 'projects#show'
scope '/admin' do
resources :projects do
resources :pictures
end
end
end
projects_controller.rb
layout 'adminApplication'
before_action :set_project, only: [:show, :edit, :update, :destroy]
def index
#projects = Project.all
end
def show
end
def new
#project = Project.new
end
def edit
end
def create
#project = Project.new(project_params)
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
def destroy
#project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_project
#project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:name, :date, :location, :integer)
end
end
front_controller.rb
def index
#projects = Project.all
render 'projects/index'
end
def show
end
def profile
end
end
in projects/index.html.erb
- link_to 'Show', project
- link_to 'Show', front_path(project)
I already checked all similar questions.
Thanks for your help !
Kazei Design
Update
rake routes | grep front:
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
You're using a named route helper, but you didn't specify it:
- link_to 'Show', front_path(project)
And you can see in your routes that front_path for projects#show doesn't exist:
rake routes | grep front
front GET /front(.:format) front#index
front_profile GET /front/profile(.:format) front#profile
GET /front/:id(.:format) projects#show
So, in your routes.rb add the helper:
get 'front/:id' => 'projects#show', as: :custom_front
And now run rake routes to see the new helper (it should be custom_front_path) and use it:
- link_to 'Show', custom_front_path(project)
See more info in the documentation - 4.3 Overriding the Named Helpers
You might want to change your route to
get 'front.:id' => 'projects#show'

Nested Namespace Update Action No route matches [PATCH]

Been trying to progress through rails using namspacing, there's been similar questions I think my problem is everything is namespaced.
I tried adding a class to the models (did that before with devise user with nest object and worked) but it doesn't seem to work when dealing with namespaces.
I get a routing error.
No route matches [PATCH] "/backend/membercontacts/1/memberlistings"
routes.rb
# Application Client Backend
namespace :backend do
# Member Routes
resources :membercontacts do
resources :memberaddresses
resources :memberlistings
end
end
backend/memberlisting.rb
class Backend::Memberlisting < ActiveRecord::Base
# Model Relationships
belongs_to :membercontact
end
backend/membercontact.rb
class Backend::Membercontact < ActiveRecord::Base
# Model Relationship
has_many :memberlistings, dependent: :destroy
end
backend/memberlistings_controller.rb
class Backend::MemberlistingsController < ApplicationController
# Security & Action Filters
layout '/backend/application.html.erb'
before_action :set_memberlisting, only: [:show, :edit, :update, :destroy]
# Member Listing Index
def index
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlistings = membercontact.memberlistings.order('mlcontactname ASC')
end
# Detailed Member Listing Profile
def show
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#membercontact = membercontact.memberlistings.find(params[:id])
end
# New Member Listing
def new
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = membercontact.memberlistings.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #membercontact }
end
end
# Edit Member Listing
def edit
membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = membercontact.memberlistings.find(params[:id])
end
# Create Member Listing Action
def create
#membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = #membercontact.memberlistings.create(memberlisting_params)
respond_to do |format|
if #memberlisting.save
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Created.' }
format.json { render action: 'show', status: :created, location: #memberlisting }
else
format.html { render action: 'new' }
format.json { render json: #memberlisting.errors, status: :unprocessable_entity }
end
end
end
# Update Member Listing Action
def update
respond_to do |format|
if #memberlisting.update(memberlisting_params)
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #memberlisting.errors, status: :unprocessable_entity }
end
end
end
# Delete Member Listing Action
def destroy
#membercontact = Backend::Membercontact.find(params[:membercontact_id])
#memberlisting = #membercontact.memberlistings.find(params[:id])
#memberlisting.destroy
respond_to do |format|
format.html { redirect_to backend_membercontact_memberlistings_path, notice: 'Address for Membercontact was Successfully Deleted.' }
format.json { head :no_content }
end
end
private
def set_memberlisting
#memberlisting = Backend::Memberlisting.find(params[:id])
end
def memberlisting_params
params.require(:backend_memberlisting).permit(:mlcontactname, :mlcompanyname, :mladdressline1, :mladdressline2, :mlcity, :mlprovince, :mlpostalcode, :mlphone, :mlwebsite, :mlemail, :membercontact_id, :account_id)
end
end
Routes Rake
backend_membercontact_memberlistings_path GET /backend/membercontacts/:membercontact_id/memberlistings(.:format) backend/memberlistings#index
POST /backend/membercontacts/:membercontact_id/memberlistings(.:format) backend/memberlistings#create
new_backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/new(.:format) backend/memberlistings#new
edit_backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/:id/edit(.:format) backend/memberlistings#edit
backend_membercontact_memberlisting_path GET /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#show
PATCH /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#update
PUT /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#update
DELETE /backend/membercontacts/:membercontact_id/memberlistings/:id(.:format) backend/memberlistings#destroy
So far I've tried to work with classes (still learning) in the models file and have tried some variations like below. Also the reason why I define the url path, it defaulted to backend_membercontact_backend_memberlisting
, class_name: 'Backend::Memberlisting'
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlistings_path, method: :post) do |f| %>
No route matches [PATCH] "/backend/membercontacts/1/memberlistings"
From your rake routes output, the path is backend_membercontact_memberlisting_path( no s memberlisting) not backend_membercontact_memberlistings_path
Also, you should change method: post to method: :patch
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlisting_path, method: :patch) do |f| %>
Try to use
<%= form_for([#memberlisting.membercontact, #memberlisting], url: backend_membercontact_memberlisting_path, method: :post) do |f| %>
Your are using backend_membercontact_memberlistings_path according to your routes is should be backend_membercontact_memberlisting_path
Hope this works.

NoMethodError : undefined method `find' for nil:NilClass

I want show a daycare details on show page but I got this error
NoMethodError : undefined method `find' for nil:NilClass
from daycare controller file and i'm not get any idea. I have mentioned below that error line.
This is my Controller file
class DayCaresController < ApplicationController
before_filter :authenticate_user!
before_action :set_day_care, only: [:show, :edit, :update, :destroy]
# GET /day_cares
# GET /day_cares.json
def index
#day_cares = DayCare.all
end
# GET /day_cares/1
# GET /day_cares/1.json
def show
end
# GET /day_cares/new
def new
#day_care = DayCare.new
end
# GET /day_cares/1/edit
def edit
end
# POST /day_cares
# POST /day_cares.json
def create
#day_care = current_user.build_day_care(day_care_params)
respond_to do |format|
if #day_care.save
UserMailer.welcome_email(#user).deliver
format.html { redirect_to #day_care, :gflash => { :success => 'Day care was successfully created.'} }
format.json { render :show, status: :created, location: #day_care }
else
format.html { render :new }
format.json { render json: #day_care.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /day_cares/1
# PATCH/PUT /day_cares/1.json
def update
respond_to do |format|
if #day_care.update(day_care_params)
format.html { redirect_to #day_care, :gflash => { :success => 'Day care was successfully updated.'} }
format.json { render :show, status: :ok, location: #day_care }
else
format.html { render :edit }
format.json { render json: #day_care.errors, status: :unprocessable_entity }
end
end
end
# DELETE /day_cares/1
# DELETE /day_cares/1.json
def destroy
#day_care.destroy
respond_to do |format|
format.html { redirect_to day_cares_url, :gflash => { :success => 'Day care was successfully destroyed.'} }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions
def set_day_care
#day_care = current_user.day_care.find(params[:id]) # => **I got error this line**
end
# Never trust parameters from the scary internet, only allow the white list through.
def day_care_params
params.require(:day_care).permit(:name, :address, :office_phone, :cell_phone, :logo, :website, :user_id)
end
def dashboard
end
def profile
end
end
If user has_many: day_cares then use this name instead of day_care:
#day_care = current_user.day_cares.where(id: params[:id]).take
or probably as you wrote:
#day_care = current_user.day_cares.find(params[:id])
But with arrays instead of single instance (day_cares).
Also you can use just:
#day_care = DayCare.find(params[:id])
If you search by id. Or if you need to check that it's users day_care:
#day_care = DayCare.where(id: params[:id], user: current_user).take
current_user.day_care.find is not available, because you can only perform queries on plural associations. So given that the model associations are setup correctly as:
class User < ActiveRecord:Base
has_many :day_cares
...
end
the solution is probably just to resolve the spelling error from
`current_user.day_care.find` #wrong!
to
`current_user.day_cares.find` #right!

Generating scaffolding for existing model

Newbie to RoR here. I have built models with no namespace. One of them is called 'Brand'. I then proceeded to use rails g "admin/brands" to put maintenance functionality under an admin namespace, using rails generate scaffolding_controller "admin/brand" - which produced the views and the controller. The unit tests fail when I rake test:
NoMethodError: undefined method `admin_brands' for #<Admin::BrandsControllerTest:0x1034c0730>
test/functional/admin/brands_controller_test.rb:5:in `_callback_before_193'
in routes.rb I have:
# Administration routes
namespace :admin do
resources :brands
end
The generated controller code is as follows:
class Admin::BrandsController < ApplicationController
# GET /admin/brands
# GET /admin/brands.json
def index
#admin_brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #admin_brands }
end
end
# GET /admin/brands/1
# GET /admin/brands/1.json
def show
#admin_brand = Brand.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/new
# GET /admin/brands/new.json
def new
#admin_brand = Brand.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/1/edit
def edit
#admin_brand = Brand.find(params[:id])
end
# POST /admin/brands
# POST /admin/brands.json
def create
#admin_brand = Brand.new(params[:admin_brand])
respond_to do |format|
if #admin_brand.save
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully created.' }
format.json { render :json => #admin_brand, :status => :created, :location => #admin_brand }
else
format.html { render :action => "new" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# PUT /admin/brands/1
# PUT /admin/brands/1.json
def update
#admin_brand = Brand.find(params[:id])
respond_to do |format|
if #admin_brand.update_attributes(params[:admin_brand])
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /admin/brands/1
# DELETE /admin/brands/1.json
def destroy
#admin_brand = Brand.find(params[:id])
#admin_brand.destroy
respond_to do |format|
format.html { redirect_to admin_brands_url }
format.json { head :no_content }
end
end
end
Not sure how to debug this type of issue... I gather that paths are messed up somehow, but that is much as I can fathom at this point. Help appreciated.
In our company we dont use scaffold, especially when we need to generate admin namespace.
You can simply write admin namespace yourself.
config/routes.rb
namespace :admin do
root :to => "base#index"
resources :pages
# resources :states do
# member do
# get :make_default
# end
# end
end
app/controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
before_filter :authenticate_user!, :admin_user?
layout "admin/admin"
def index
#page = Page.all
end
private
def admin_user?
redirect_to root_path, :alert => 'This page is allowed for admin' unless current_user.admin
end
end
app/views/admin/base/index.html.haml
= link_to "New Post", new_admin_post_path
%ul
- #post.each do |post|
%li= post.title

Routing in Rails making the Username an URL:

In my Rails App there is Device Model - User, and a Registry model( Each user has one registry).
I wanted to change my routes so that instead of:
"http://localhost:3000/registries/3"
it shows:
"http://localhost:3000/erinwalker"
So I changed routes to
match '/:name' => "registries#show"
And the show action in my controller to:
def show
#user = current_user
#user = User.find_by_name!(params[:name])
#registry = #user.registry
And it works, but when I create or update the registry now first it says:
Couldn't find User with name =
app/controllers/registries_controller.rb:21:in `show'
Even though the show action works?
The registries controller:
class RegistriesController < ApplicationController
before_filter :authenticate_user!, :except => :show
load_and_authorize_resource
# GET /registries
# GET /registries.json
def index
#registries = Registry.all
#user = current_user
respond_to do |format|
format.html # index.html.erb
format.json { render json: #registries }
end
end
# GET /registries/1
# GET /registries/1.json
def show
#user = current_user
#user = User.find_by_name!(params[:name])
#registry = #user.registry
respond_to do |format|
format.html # show.html.erb
format.json { render json: #registry }
end
end
# GET /registries/new
# GET /registries/new.json
def new
#registry = Registry.new
#user = current_user
respond_to do |format|
format.html # new.html.erb
format.json { render json: #registry }
end
end
# GET /registries/1/edit
def edit
#registry = Registry.find(params[:id])
#user = current_user
end
# POST /registries
# POST /registries.json
def create
#registry = current_user.build_registry(params[:registry])
#user = current_user
respond_to do |format|
if #registry.save
format.html { redirect_to #registry, notice: 'Registry was successfully created.' }
format.json { render json: #registry, status: :created, location: #registry }
else
format.html { render action: "new" }
format.json { render json: #registry.errors, status: :unprocessable_entity }
end
end
end
# PUT /registries/1
# PUT /registries/1.json
def update
#registry = Registry.find(params[:id])
#user = current_user
respond_to do |format|
if #registry.update_attributes(params[:registry])
format.html { redirect_to #registry, notice: 'Registry was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #registry.errors, status: :unprocessable_entity }
end
end
end
All my Routes:
Mystorkparty::Application.routes.draw do
devise_for :users
resources :registries
root :to => "static_pages#home"
match '/home', to: 'static_pages#home'
match '/:name' => "registries#show"
When you create or update your models, you send POST /registries or PUT /registries/1.
But /registries is matched by your last rule match '/:name' => "registries#show", so the request hits the show action.
If you run rake routes you should see something like this:
POST /registries(.:format) registries#create
PUT /registries/:id(.:format) registries#update
DELETE /registries/:id(.:format) registries#destroy
/:name(.:format) registries#show
You can add method parameter to your route, so that it will hit show only on GET request.
match '/:name' => "registries#show", :via => :get
But there are still can be collisions in the future. For example, if you have registry name users.
So, it's commonly suggested to use prefixes (match '/r/:name') or define set of allowed names, or choose safe names for registries.
P.S. I don't think load_and_authorize_resource will work for your show action by default. Because it expects params[:id] to load the resource automatically.

Resources