Rails routes not matching 'new' pattern with RESTFul routes - ruby-on-rails

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

Related

Rails: Sub-routing. No route matches

I have custom CRUD routes for example - for profiles
get '/profiles', to: 'profiles#index'
get '/profiles/new', to: 'profiles#new', :as => 'new_profile'
post '/profiles', to: 'profiles#create'
get '/profiles/edit/:id', to: 'profiles#edit', :as => 'profile'
patch '/profiles/edit/:id', to: 'profiles#update'
get '/profiles/get_profiles', to: 'profiles#get_profiles'
It works okay. But I do the same routing for profile skills, wich is under relation of profile. Routes of ProfileSkills looks like this
get '/profiles/:profile_id/profile_skills', to: 'profile_skills#index'
get '/profiles/:profile_id/profile_skills/new', to: 'profile_skills#new', :as => 'new_profile_skill'
post '/profiles/:profile_id/profile_skills', to: 'profile_skills#create'
get '/profiles/:profile_id/profile_skills//edit/:id', to: 'profile_skills#edit', :as => 'profile_skills'
patch '/profiles/:profile_id/profile_skills/edit/:id', to: 'profiles#update'
When I under the route for creating the new item
http://localhost:3000/profiles/1/profile_skills/new
It throw an exception
No route matches {:action=>"edit", :controller=>"profile_skills", :profile_id=>"1"}, missing required keys: [:id]
On form for line
<%= form_for #profile_skill do |form| %>
Why he don't understand that I'm under the 'new' route and it looking for 'edit', when i'm under the 'new'?
This problem is only when i'm on sub-routes. In 'Porfile' routes for example, if works fine.
In your routes use this
resources :profiles do
resources :profile_skills
end
this will provide you the routes like this
profiles/:profile_id/profile_skill points to index action of profile_skill
profiles/:profile_id/profile_skill/new points to new action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id points to show action of profile_skill
profiles/:profile_id/profile_skill/:profile_skill_id/edit points to edit action of profile_skill
and so on.
for more help visit Rails Routing

Building custom route in rails

I have this route:
put 'workstations' => 'workstation#update'
that I'm intending to match up with this link:
link_to "Update", controller: "workstations", action: "update", :method => :put
but I'm getting this error and I don't know why:
No route matches {:controller=>"workstations", :action=>"update", :method=>:put}
How can I build the route and why isn't what I've specified working?
It might be a spelling error.
You wrote:
put 'workstations' => 'workstation#update'
But the controller name should be plural as in:
put '/workstations' => 'workstations#update'
Also, you can always run rake routes and get the list of generated routes and route names in your app.
EDIT
By the way, you can achieve the same routes generation with the following, more elegant way:
resource :workstations, only: :update

Rails Routes Resources Member Issue

I have created a nested routes like this
resources :subjects do
resources :subject_edits do
member do
post :vote
end
end
end
when I run rake routes, I found it exited
vote_subject_subject_edit POST /subjects/:subject_id/subject_edits/:id/vote(.:format) subject_edits#vote
but when I use it in my .erb file
<%= button_to 'I Agree', :action => vote_subject_subject_edit_path(#subject, #edit) %>
I got an error,
No route matches {:action=>"/subjects/25/subject_edits/1/vote",
:subject_id=>"25", :id=>"1", :controller=>"subject_edits"}
what is wrong here?
This helper sets url explicitly, so you need:
<%= button_to 'I Agree', vote_subject_subject_edit_path(#subject, #edit) %>
without passing :action option.

Rails routes for get request with query params

I need a route to accept a request reports#show with an attached :query parameter and I can't figure out how to write it. It needs to respond to this link in my view:
= link_to report_path(query: params[:query]) do
config/routes.rb
resources :reports do
resources :chapters
resources :pages
end
Tried variations of: get '/reports/:id/:query', :as => 'reports_query' but I keep getting:
Routing Error
No route matches {:action=>"show", :controller=>"reports", :query=>"europe"}
Project is mostly RESTful but I'll take anything that works at this point. Thanks for any help.
You should define your route to query with code like this
# routes.rb
resources :reports do
get ':query', to: 'reports#show', on: :member, as: :query
end
It will generate path helper you can use that way
= link_to 'Query Report', query_report_path(#report, query)
I went through the same problem here, and I solved it using the default param while defining my routes.
get :twitter_form, defaults: { form: "twitter" }, as: :twitter_form, to: "campaigns#show"

link_to "about",about_path showing the error in ROR

i am working in Ruby on rails.. i am new to this..
i have used a line
<%= link_to "about",about_path %>
which throws me a error as,
undefined local variable or method `about_path' for #<ActionView::Base:0xb5f5baa8>
i am having the page about.html under app/views/pages/
please give some suggestions of why i am getting like this .
I just had the same problem using the Hartl's tutorial. Here is what I did.
When asking rake routes, I have :
tomsihap-MBP:sample_app tomsihap$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact
Then the correct path is :
<%= link_to "About", static_pages_about_path %>
And not <%= link_to "About", about_path %> as suggested by Hartl's guide.
EDIT :
Ok now I understand. That is because routes were defined like that :
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Instead of, later explained into the tutorial :
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
Using this way, the correct path now is :
<%= link_to "About", about_path %>
Your code is looking for what is called a named route. You need to define these in config/routes.rb. In addition you will need some controller and action to handle them. See this post describing a very simple way to handle static pages by way of illustration.
To get the about_path named route, then you would add this to routes.rb
map.about "/pages/about", :controller => "pages", :action => "show", :id => "about"
Then add your about page contents to a file called app/views/pages/about.html.erb
Finally:
$ rake routes
tells you all of the named routes defined for your application and what they do
I guess your about page is "static". Check this..
routes.rb
# rails 2.3.x
map.about "/pages", :controller => 'pages', :action => 'about'
Controllers/pages_controller.rb
class PagesController < ApplicationController
def about # not needed, only for "tidiness"
end
end
... and your erb file have to be here: Views\pages\about.html.erb
Is in your routes.rb something like map.resources :about ?
If you don't know why it should be there or what is that, read about RESTful Routing on guides.

Resources