No route matches {:action=>"show", :controller=>"controller_name"} - ruby-on-rails

I'm always getting this error and I don't understand where I am wrong. I think I have everything I need action in controller, resources in route file and view for controller action. I put the route current_events/new in the browser when I get this error. I also try with just resources :current_events
output of rake routes:
current_events GET /current_events(.:format) current_events#index
new_current_event GET /current_events/new(.:format) current_events#new
current_event GET /current_events/:id(.:format) current_events#show
config/routes.rb:
appname::Application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: "omniauth_callbacks"}
resources :current_events, only: [:show, :new, :index]
end
CurrentEventsController:
class CurrentEventsController < ApplicationController
def index
#current_event = CurrentEvent.all
respond_to do |format|
format.html
format.json { render json: #current_event }
end
end
def create
#current_event = CurrentEvent.new(params[:current_event])
respond_to do |format|
if #current_event.save
format.html { redirect_to #current_event, notice: 'current event was created.' }
format.json { render json: #current_event, status: :created, location: #current_event }
else
format.html { render action: "new" }
format.json {render json: #current_event.errors, status: :unprocessable_entity}
end
end
end
def new
#current_event = CurrentEvent.new
respond_to do |format|
format.html
format.json { render json: #current_event }
end
end
def edit
#current_event = CurrentEvent.find(params[:id])
end
def destroy
#current_event = CurrentEvent.find(params[:id])
#current_event.destroy
respond_to do |format|
format.html { redirect_to current_event_url}
format.json { head :no_content }
end
end
def show
#current_event = CurrentEvent.find(params[:id])
respond_to do |format|
format.html
format.json{ render json: #current_event}
end
end
end

I forgot to say, I am trying to go to new page so in browser I say current_events/new
It will still throw an error if there's a link that doesn't work on the page.
In your view, the link should look something like this:
<%= link_to "name of current event", current_event_path(#current_event) %>
update
based on your rake routes
current_event GET /current_events/:id(.:format) #note ":id"
when you're trying to see a specific current event, you need to pass it an :id which makes sense - if you're trying to call a specific person you need to use their telephone number. so your code should look like this:
<%= link_to 'Back', current_event_path(#event) %>
But keep in the mind that #event won't work unless you define it correctly in the controller action for this view.

Related

Rails 5.1 Trying to pass an attribute to a form using a link

I am trying to pass an attribute to an object that is being created by a link. I am on the show view of another object and I want to have two links available one that will make the :attribute false and the other to make the :attribute true. I have it set up so the default value of the this attribute is false and I tried using something like below, but it just saves it as nil in the database:
<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(#building, #listing, #appointment, #rented_unit, leased: true) %>>
controller
class RentedUnitsController < ApplicationController
before_action :building
before_action :listing
before_action :appointment
before_action :set_rented_unit, only: [:show, :edit, :update, :destroy]
# GET /rented_units
# GET /rented_units.json
def index
#rented_units = appointment.rented_units
end
# GET /rented_units/1
# GET /rented_units/1.json
def show
end
# GET /rented_units/new
def new
#rented_unit = appointment.rented_units.new
end
# GET /rented_units/1/edit
def edit
end
# POST /rented_units
# POST /rented_units.json
def create
#rented_unit = appointment.rented_units.new(rented_unit_params)
respond_to do |format|
if #rented_unit.save
format.html { redirect_to [building, listing, appointment, #rented_unit], notice: 'Rented unit was successfully created.' }
format.json { render :show, status: :created, location: #rented_unit }
else
format.html { render :new }
format.json { render json: #rented_unit.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /rented_units/1
# PATCH/PUT /rented_units/1.json
def update
respond_to do |format|
if #rented_unit.update(rented_unit_params)
format.html { redirect_to [building, listing, appointment, #rented_unit], notice: 'Rented unit was successfully updated.' }
format.json { render :show, status: :ok, location: #rented_unit }
else
format.html { render :edit }
format.json { render json: #rented_unit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rented_units/1
# DELETE /rented_units/1.json
def destroy
#rented_unit.destroy
respond_to do |format|
format.html { redirect_to building_listing_appointment_rented_units_path(#building, #listing, #appointment), notice: 'Rented unit was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_rented_unit
#rented_unit = appointment.rented_units.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def rented_unit_params
params.require(:rented_unit).permit(:unit_no, :unit_model, :price, :bedrooms, :bathrooms, :half_baths, :square_footage, :leased, :appointment_id)
end
def building
#building ||= Building.find(params[:building_id])
end
def listing
#listing ||= Listing.find(params[:listing_id])
end
def appointment
#appointment ||= Appointment.find(params[:appointment_id])
end
end
From what I understand you are looking to populate leased attribute auto when you open a new from from the link.
You need to give the param param to the link.
<%= link_to "Yes", new_building_listing_appointment_rented_unit_path(#building, #listing, #appointment, #rented_unit, rented_unit: { leased: true } ) %>>
In the controller then you can do some thing like
# GET /rented_units/new
def new
#rented_unit = appointment.rented_units.new(rented_unit_params)
end
Then, in the new form you will see the checkbox (or other control) selected.

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.

Association not recognized, undefined method error

I created 2 scaffolds 'mepager' and 'pimp' and linked the models like this:
class Mepager < ActiveRecord::Base
belongs_to :pimp
end
class Pimp < ActiveRecord::Base
has_one :mepager
end
I added following lines to my routing:
resources :pimps do
resources :mepagers
end
And my mepager create action looks like this:
def create
#mepager = #pimp.build_mepager(mepager_params)
respond_to do |format|
if #mepager.save
format.html { redirect_to #mepager, notice: 'Mepager was successfully created.' }
format.json { render action: 'show', status: :created, location: #mepager }
else
format.html { render action: 'new' }
format.json { render json: #mepager.errors, status: :unprocessable_entity }
end
end
end
And for #pimp to set
def setPimp
#pimp = Pimp.find_by_id(:pimp_id)
end
But if I try to create a new mepager at pimps/1/mepagers/new I get an undefined method build_mepager error.
undefined method `build_mepager' for nil:NilClass
I tried the same in the rails console and it worked with just that method. So I guess somethings still missing to make it work on my web application.
Btw I m using rails 4.0.0
Regards!
Routing
Prefix Verb URI Pattern Controller#Action
pimps GET /pimps(.:format) pimps#index
POST /pimps(.:format) pimps#create
new_pimp GET /pimps/new(.:format) pimps#new
edit_pimp GET /pimps/:id/edit(.:format) pimps#edit
pimp GET /pimps/:id(.:format) pimps#show
PATCH /pimps/:id(.:format) pimps#update
PUT /pimps/:id(.:format) pimps#update
DELETE /pimps/:id(.:format) pimps#destroy
root GET / pimps#index
pimp_mepagers GET /pimps/:pimp_id/mepagers(.:format) mepagers#index
POST /pimps/:pimp_id/mepagers(.:format) mepagers#create
new_pimp_mepager GET /pimps/:pimp_id/mepagers/new(.:format) mepagers#new
edit_pimp_mepager GET /pimps/:pimp_id/mepagers/:id/edit(.:format) mepagers#edit
pimp_mepager GET /pimps/:pimp_id/mepagers/:id(.:format) mepagers#show
PATCH /pimps/:pimp_id/mepagers/:id(.:format) mepagers#update
PUT /pimps/:pimp_id/mepagers/:id(.:format) mepagers#update
DELETE /pimps/:pimp_id/mepagers/:id(.:format) mepagers#destroy
GET /pimps(.:format) pimps#index
POST /pimps(.:format) pimps#create
GET /pimps/new(.:format) pimps#new
GET /pimps/:id/edit(.:format) pimps#edit
GET /pimps/:id(.:format) pimps#show
PATCH /pimps/:id(.:format) pimps#update
PUT /pimps/:id(.:format) pimps#update
DELETE /pimps/:id(.:format) pimps#destroy
Altered line in pimp controller
def create
#pimp = Pimp.new(pimp_params)
respond_to do |format|
if #pimp.save
format.html { redirect_to new_pimp_mepager_path, notice: 'Product Improvement was successfully created.' }
format.json { render action: 'show', status: :created, location: #pimp }
else
format.html { render action: 'new' }
format.json { render json: #pimp.errors, status: :unprocessable_entity }
end
end
end
mepager controller
class MepagersController < ApplicationController
before_action :set_mepager, only: [:show, :edit, :update, :destroy]
def setPimp
#pimp = Pimp.find(params[:pimp_id])
end
# GET /mepagers
# GET /mepagers.json
def index
#mepagers = Mepager.all
end
# GET /mepagers/1
# GET /mepagers/1.json
def show
end
# GET /mepagers/new
def new
#mepager = Mepager.new
end
# GET /mepagers/1/edit
def edit
end
# POST /mepagers
# POST /mepagers.json
def create
raise params[:pimp_id].inspect
#mepager = #pimp.build_mepager(mepager_params)
respond_to do |format|
if #mepager.save
format.html { redirect_to #mepager, notice: 'Mepager was successfully created.' }
format.json { render action: 'show', status: :created, location: #mepager }
else
format.html { render action: 'new' }
format.json { render json: #mepager.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /mepagers/1
# PATCH/PUT /mepagers/1.json
def update
respond_to do |format|
if #mepager.update(mepager_params)
format.html { redirect_to #mepager, notice: 'Mepager was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #mepager.errors, status: :unprocessable_entity }
end
end
end
# DELETE /mepagers/1
# DELETE /mepagers/1.json
def destroy
#mepager.destroy
respond_to do |format|
format.html { redirect_to mepagers_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_mepager
#mepager = Mepager.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def mepager_params
params.require(:mepager).permit(:pre, :post, :comment, :save_h, :save_c, :save_other, :save_otherv, :affect_design, :affect_stress, :affect_me, :affect_other, :affect_dno, :affect_mid, :affect_otherdoc, :owner, :pps, :reference)
end
end
The problem is probably here:
#pimp = Pimp.find_by_id(:pimp_id)
what you are doing is finding a pimp that has an id of the literal symbol :pimp_id (which is highly unlikely to exist)
Probably what you want is the parameter: params[:pimp_id]
EDIT:
and for the second part.
for the create action to work with a real #pimp (that can be found by :pimp_id), you must also do the same on the new action too!
Otherwise create won't work.
Can I recommend something like...
before_action :fetch_pimp, only: [:new, :create]
# GET /mepagers/new
def new
#mepager = Mepager.new
end
# POST /mepagers
# POST /mepagers.json
def create
#mepager = #pimp.build_mepager(mepager_params)
respond_to do |format|
# as previously...
end
end
# ...
private
def fetch_pimp
#pimp = Pimp.find(params[:pimp_id])
end

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