ROR Form_with keeps giving me a NoMethodError - ruby-on-rails

I'm sure this should be a simple issue, but can't find a solution (im new to ROR).
This is the error im getting:
-----error------
NoMethodError in Agency::Clientmanagement#edit
Showing /home/ubuntu/environment/nacho/app/views/agency/clientmanagement/_form_client.html.erb where line #2 raised:
undefined method `client_path' for #ActionView::Base:0x007fcda8bd7ae0
Did you mean? clients_path
<%= form_with model: #clientrecord do |f| %>
------ This is my routes------
root 'pages#home'
get 'clients', to: 'clients#index'
get 'agency', to: 'agency#index'
get 'admin', to: 'admin#index'
get 'clientview', to: 'agency#clientview'
#post 'clientview', to: 'agency#clientview'
namespace :admin do
get "new", to: 'manager#new'
get "add-user", to: 'manager#add-user'
get "agencies", to: 'manager#agencies'
end
namespace :agency do
resources :clientmanagement, only: [:show,:new, :create, :edit]
resources :team, only: [:index, :show,:new, :create, :edit]
end
-------this is the: admin/clientmanagementcontroller
class Agency::ClientmanagementController < ApplicationController
before_action :require_agencyaccount
before_action :user_clients
before_action :set_client, only: [:show]
# before_action :set_one_product, only: [:show]
# def index
# #all_shoe_products = #all_products.where(main_category_id: MainCategory.find_by_name("shoes").id)
# end
def show
end
def new
end
def create
end
def edit
#clientrecord = Client.find(params[:id])
end
private
def user_clients
#clients = User.find(current_user.id).clients
end
def set_client
#client = Client.find(params[:id])
end
--- here is the form_with in the form partial being rendered in /agency/clientmanagement/{id}/edit
<%= form_with model: #clientrecord do |f| %>
Any help would be greatly appreciated

In your routes.rb file I don't see client_path is defined.
Option 1: You can add client path by adding the following route below clients path:
Rails.application.routes.draw do
get 'clients', to: 'clients#index'
get 'client', to: 'clients#show'
...
end
this will generate the following path:
client_path GET /client(.:format) clients#show
Option 2: If you have a full resource controller for clients, you can add
resources :clients
this will generate all RESTful routes for clients
clients_path GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client_path GET /clients/new(.:format) clients#new
edit_client_path GET /clients/:id/edit(.:format) clients#edit
client_path GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy

Related

Rails routing to nested controller

I have one controller for creating category, and I want to have one more nested controller to create language versions of categories. I want to avoid methods like new_language, edit_language, ... and routing on them, I would like to do it the best rails way. But I am new to rails (from Padrino and Sinatra), and I am little bit lost in routing.
I have my controller for categories
module Admin
class CategoriesController < ApplicationController
before_action :authenticate_user!
before_action :find_category, only: [:show, :edit, :update]
layout 'admin'
def index
#categories = Category.all
end
..... all the others CRUD methods ....
end
end
And another controller.
module Admin
class CategoriesLanguageController < ApplicationController
before_action :authenticate_user!
before_action :find_category, only: [:show, :edit, :update]
def new
#category = Category.find(params[:category].to_i)
end
.... all the others CRUD methods ...
end
end
and my routes config.
Rails.application.routes.draw do
devise_for :users
resources :users
namespace :admin do
get '/' => 'dashboard#index'
resources :dashboard, only: [:index]
resources :categories do
collection do
get :publish_category
#get :new_lang
#post :create_lang
#get :edit
#post :update
#get :destroy
end
resources :language, only: [:new, :create, :edit]
end
end
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root to: "homepage#index"
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :categories, only: [:index]
end
end
end
I would like to have methods new_lang and create_lang under second controller with default names new, create, ....
It generates this
admin GET /admin(.:format) admin/dashboard#index
admin_dashboard_index GET /admin/dashboard(.:format) admin/dashboard#index
publish_category_admin_categories GET /admin/categories/publish_category(.:format) admin/categories#publish_category
admin_category_language_index POST /admin/categories/:category_id/language(.:format) admin/language#create
new_admin_category_language GET /admin/categories/:category_id/language/new(.:format) admin/language#new
edit_admin_category_language GET /admin/categories/:category_id/language/:id/edit(.:format) admin/language#edit
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
but it is not working.
ActionController::UrlGenerationError in Admin::Categories#index
and error is this route:
<td><%= link_to cat.internal_name, edit_admin_category_language_path(id: cat) %></td>
And I don't know how to create these actions in another controller and use url helpers.
Or this is not the best way to do it?
Please any advice?

Template is missing for request formats

I am getting this error:
StaticController#home is missing a template for request formats: text/html
This is my controller:
class StaticController < ApplicationController
def home
# render json: {status: "is working"}
end
end
When I uncomment the comment above, it renders that.
my views/static/home.html.erb view:
<%= javascript_pack_tag 'index' %>
and my routes:
Rails.application.routes.draw do
resources :sessions, only: [:create]
resources :signup, only: [:create]
delete :logout, to: 'sessions#logout'
get :logged_in, to: 'sessions#logged_in'
root to: "static#home"
# get '*path', to: "static#home", via: :all
end

Rails Route Redirecting to different view by default

The following is my routes.rb
Rails.application.routes.draw do
resources :admin do
post 'user_creation', on: :collection
end
get 'admin/user_creation'
resources :admin, :only => [:user_creation, :create, :new]
end
the following is my controller
class AdminController < ApplicationController
def new
#dashboard_user = DashboardUser.new
end
def create
#dashboard_user = DashboardUser.new(dashboard_params)
#dashboard_user.save!
end
def user_creation
end
private
def dashboard_params
params.require(:dashboard_user).permit(:user_name, :password, :last_name, :first_name, :middle_name , :phone)
end
end
then its a simple .html.erb file
what happens is that when i try to open the link admin/user_creation it automatically redirects to show.html.erb file
rake routes output
rake routes
Prefix Verb URI Pattern Controller#Action
user_creation_admin_index POST /admin/user_creation(.:format) admin#user_creation
admin_index GET /admin(.:format) admin#index
POST /admin(.:format) admin#create
new_admin GET /admin/new(.:format) admin#new
edit_admin GET /admin/:id/edit(.:format) admin#edit
admin GET /admin/:id(.:format) admin#show
PATCH /admin/:id(.:format) admin#update
PUT /admin/:id(.:format) admin#update
DELETE /admin/:id(.:format) admin#destroy
admin_user_creation GET /admin/user_creation(.:format) admin#user_creation
POST /admin(.:format) admin#create
GET /admin/new(.:format) admin#new
root GET / admin#new
You are clicking the link for the url. admin/user_creation
But your route says it is a POST url .
It need to added as GET.
Rails.application.routes.draw do
resources :admin do
post 'user_creation', on: :collection
get 'user_creation', on: :collection
end
get 'admin/user_creation'
resources :admin, :only => [:user_creation, :create, :new]
end
Your route not looking good. Whenever we try to go to any url directly from browser it consider request as get type. routes file try to find route from top to bottom. That why it's considering /admin/user_creation as show action of admin_controller and user_creation as id to show.
Don't know why you need get and post request for same action. But your routes should look like this:
Rails.application.routes.draw do
resources :admin, :only => [:create, :new] do
post 'user_creation', on: :collection
get 'user_creation', on: :collection
end
end
One thing with above routes there is no route for show action. Now you will properly go to user_creation path.
if you don't need any post path then it should look like:
Rails.application.routes.draw do
resources :admin, :only => [:create, :new] do
get 'user_creation', on: :collection
end
end
if you need all the default action then:
Rails.application.routes.draw do
resources :admin do
get 'user_creation', on: :collection
end
end

New route in resource generates instead of params[:id] params[:patient_id]

I added a new route to one of my resources:
resources :patients do
get 'warte'
end
This generates:
patient_warte_path GET /patients/:patient_id/warte(.:format) patients#warte
patients_path GET /patients(.:format) patients#index
new_patient_path GET /patients/new(.:format) patients#new
edit_patient_path GET /patients/:id/edit(.:format) patients#edit
patient_path GET /patients/:id(.:format) patients#show
.....
What i dont undestand is why the new route generated params[:patient_id!
I mean in my controller i have:
before_action :set_patient, only: [:show, :edit, :update, :destroy, :warte]
def set_patient
#patient = Patient.find(params[:id])
session[:patient] = #patient.id
end
But now of course i get the error: when i call the warte action! Thanks1
Couldn't find Patient without an ID
You are missing a configuration in order to have it use the id. You need to define if it's a :member or :collection action:
resources :patients do
get 'warte', on: :member
end

Inserting variable into routes - map.resources :posts, :as => X - is this possible?

Ok, so I am working on a blog application of sorts. Thus far, it allows for a user to sign up for their own account, create posts, tags, comments, etc.
I have just implemented the ability to use www.myapp.com/brandon to set #user to find by username and therefore correctly display the users information at each url. So when you go to www.myapp.com/brandon you see all Brandon's posts, tags, and comments associated with those posts, etc. Works great.
I'm implementing this URL mapping through the routes.rb file by adding the following:
map.username_link '/:username', :controller => 'posts', :action => 'index'
And then just setting the #user variable in the PostController and corresponding views to find_by_username. Now the issue is this. Once at www.myapp.com/brandon when you click on a post title, it sends to myapp.com/posts/id without the username in the URL.
How do I tell rails to replace the /posts with /username.
Is it even possible to insert the user_username variable into this code?
map.resources :posts, :as => [what goes here]
You said there's going to be more than just posts on the page? comments and tags too? Sounds like we need some resource aggregation here...
Another concern: what if a user picks the name faq and you want domain.com/faq down the road? You can't possibly know all the URLs you will want in the future. Prefixing paths with /profiles is a great way to build a little "namespace" to prevent this from happening. So...
Why not a ProfilesController?
script/generate controller profiles index show
routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :profiles, :only => [:index, :show] do |profile|
profile.resources :posts, :only => [:index, :show]
profile.resources :comments, :only => [:index, :show]
profile.resources :tags, :only => [:index, :show]
end
# ...
end
This will give you the following routes
profiles GET /profiles(.:format) {:controller=>"profiles", :action=>"index"}
profile GET /profiles/:id(.:format) {:controller=>"profiles", :action=>"show"}
profile_posts GET /profiles/:profile_id/posts(.:format) {:controller=>"posts", :action=>"index"}
profile_post GET /profiles/:profile_id/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
profile_comments GET /profiles/:profile_id/comments(.:format) {:controller=>"comments", :action=>"index"}
profile_comment GET /profiles/:profile_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
profile_tags GET /profiles/:profile_id/tags(.:format) {:controller=>"tags", :action=>"index"}
profile_tag GET /profiles/:profile_id/tags/:id(.:format) {:controller=>"tags", :action=>"show"}
profiles_controller.rb
class ProfilesController < ApplicationController
# show all profiles; profile browser
# /profiles
def index
end
# show one profile
# /profiles/:id
def show
#user = User.find_by_username(params[:id])
end
end
posts_controller.rb (and others)
class PostsController < ApplicationController
before_filter :find_profile, :only => [:index, :show]
# list all posts for this profile
# /profiles/:profile_id/posts
def index
end
# show one post for this profile
# /profiles/:profile_id/posts/:id
def show
end
protected
def find_profile
#user = User.find_by_username(params[:profile_id])
end
end
You should be able to create the link using:
= link_to "User Posts", subdomain_link_url(#user.username, #post)
In your PostController, then, I would use a before_filter to lookup and set the #user variable:
class PostController < ApplicationController
before_filter :find_user
def other_method
# Some code here
end
protected
def find_user
#user = User.find_by_username(params[:username])
end
end
I don't know much about routes and stuff, so forgive me if this doesn't make sense, but doesn't it works for you?
map.resources :posts, :path_prefix => '/:username' do |post|
post.resources :comments
end
I can see here that this will generate the following
posts GET /:username/posts(.:format) {:controller=>"posts", :action=>"index"}
POST /:username/posts(.:format) {:controller=>"posts", :action=>"create"}
new_post GET /:username/posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_post GET /:username/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
post GET /:username/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
PUT /:username/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
DELETE /:username/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
post_comments GET /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
POST /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
new_post_comment GET /:username/posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
edit_post_comment GET /:username/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
post_comment GET /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
PUT /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
DELETE /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}

Resources