Rails: ActionController::RoutingError No route matches error - ruby-on-rails

I have a search controller (no model) where I am running a query against the Users table. It works completely fine, but once i enter this
<%= link_to "Good to proceed now.", new_user_product_path, :class => "btn" %>
It gives me
ActionController::RoutingError (No route matches {:action=>"new", :controller=>"products"}):
I already have relationship estbalished between the User and product model. I am able to access the products#new when I directly go to the link http://127.0.0.1:3000/users/3/products/new. But again, when the link_to snippet is entered, it gives the above error.
My search is controller isnt tied up with the db, it just helps me process the front end.
What am i doing wrong here? Is there something I need to do with the routes?
here is my routes file
resources :searches, only: [:index, :create]
resources :users do
resources :products
end

You need to pass the user to the new_user_product_path.
So something like:
new_user_product_path(#user) or new_user_product_path(current_user)

Related

Trouble installing foreign key in child records in Rails

Problem is that Rails can't find the new route after i changed how the routes for indis (stands for individuals or 'contacts') are generated.
in my routes file as a result of this statement
resources :profiles do
resources :indis
end
these routes were generated among others:
profile_indis
GET /profiles/:profile_id/indis(.:format) indis#index
POST /profiles/:profile_id/indis(.:format) indis#create
profile is the company and indis are individuals that work at the company
so i'm sitting here looking at this which is new.html.erb for new contacts.
<h1>New Contact</h1>
<%= render 'form', indi: #indi %>
<%= link_to 'Back', :back %>
I have the following line at the top of routes to avoid the rails router using the basic create route.
resources :indis, :except => [:create]
in routes
i get
No route matches [POST] "/indis"
hint: i added the :except code because the old route wasn't valid after i made the change
resources :profiles do
resources :indis
end
now when i hit create it can't find the route and i don't know what code
<%= render 'form', indi: #indi %> generates
somehow i need to fixup the new contact code so that when they hit create it routes to the new route instead of the old one (but I don't have access to the code of the form referenced in the new contact code.

How to add params id in rails link_to

I have a Job model that contains a Company_id as a foreign key. On the company show page, I want to use a link_to tag that links to the Job new page so I can create a new job with the company_id using simple_form.
<%= link_to "Create Job", new_company_job_path %>
I get this error "No route matches {:action=>"new", :controller=>"jobs", :id=>"13"}, missing required keys: [:company_id]"
This is my nested route
resources :companies do
resources :jobs, only: [:new, :create, :update, :destroy]
end
From rails routes, this is the route to the job new page
new_company_job GET /companies/:company_id/jobs/new(.:format) jobs#new
This is the simple-form in the job_new page
<%= simple_form_for (#job) do |f| %> etc
I would like know how I can include the company_id in to the link_to tag in order to use simple_form in the job new_page to create a new job.
Rails routes can take arguments; if you ever want to explicitly pass a parameter to a route you can do so just like you would pass an argument to any other method:
<%= link_to "Create Job", new_company_job_path(company_id: #company.id) %>
*note: this assumes you have defined #company somewhere on this view.
In the case of general resource routes, Rails is smart enough to insert these params in the right place. It's worth noting though that if a param is not defined on the route in routes.rb Rails will tack on these passed parameters to the end of the route as query strings.
For example, if you have a route like
get 'landing_pages/page' => '#landing_pages#page'
and you called:
<%= link_to "Go to your landing page", landing_pages_page_path(brand: 'Apple') %>
The route will become /landing_page/page?brand=Apple
For further reference: http://guides.rubyonrails.org/routing.html

Nested routes URL path and params problems in rails

I'm simply trying to link to categories#new which is nested inside guides. I'm linking to it from the guides show page
<%= link_to "Add new category", new_guide_category_path %>
Problem is that i get the error:
No route matches {:action=>"new", :controller=>"categories", :id=>"blah"} missing required keys: [:guide_id]
It is using :id instead of :guide_id. How can i fix this so stores the param it uses :guide_id.
Or is there a different way to link to the guides/:guide_id/categories/new page and other nested resources pages
route:
resources :guides do
resources :categories, only: [:new, :create, :edit, :update]
end
Still learning rails so this might be a noob problem where you don't even use the path to link to it.
For example you have model Guide and in your controller you got variable like
#guide = Guide.where(...)
In view you need to specify id of this model and simples way to do it just to pass this variable to url helper:
<%= link_to "Add new category", new_guide_category_path(#guide) %>
And this works with multiple nested resources too. For example:
route: /foo/:foo_id/bar/:bar_id/buz/:id
Just pass all these instance of models to helper like
show_buz_bar_foo_path(#foo, #bar, #buz)
And rails will get ids of this items by itself.
Try doing this
new_guide_category_path( #guide.id )
The form_for helper can get your nesting sorted in terms of submitting data but links to other paths are a different story.

Rails routes not matching 'new' pattern with RESTFul routes

I got this error:
No route matches {:action=>"show", :controller=>"video_publications", :campaign_id=[...]
With this url:
/campaigns/514be3834413790249000025/video_publications/new
I have this in the routes:
resources :campaigns do
resources :video_publications
end
I got the error when I am redirecting to:
new_campaign_video_publication_path(#campaign)
I am confused, any ideas?
Using:
Rails 3.2.11
Mongoid 3.0.23
The error is not in your new path, but in your show action.
It is complaining about:
:action=>"show", :controller=>"video_publications"
In your code change parts of the code for show action to something like this:
<%= link_to 'Show', campaign_video_publication_path(#campaign, #publication) %>
Add this line of code in the routes.rb
match 'campaigns/:id/video_publications/new' => 'campaigns/video_publications/new',:as => :new_campaign_video_publication

Rails: restful resource routing with action_controller_path

I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.

Resources