I have a custom rails route defined as such:
resources :scores, path: "seasons/:season_id/scores/:student_id"
This makes sense for my application and would keep the urls standardized. This works for the show page and I am able to grab :season_id and :student_id properly.
However, when I try to route to any of the other pages I get errors. I wanted the new and edit page to be these routes:
New
resources :scores, path: "seasons/:season_id/scores/:student_id/new"
Edit
resources :scores, path: "seasons/:season_id/scores/:student_id/:id/edit"
So they still follow normal conventions, and that's what happens when I run rake routes, but hitting any route throws:
ActionController::UrlGenerationError
No route matches {:action=>"show", :controller=>"scores", :format=>nil, :id=>nil, :season_id=>#<Score id: ... >, :student_id=>nil} missing required keys: [:id, :student_id]
Which is weird on two accounts. The season id is being linked to the score object, and it says there are missing keys. All keys are present in the params hash, and this is how I'm building my links:
= link_to "Edit", { controller: :scores, action: :edit, id: score.id, student_id: params[:student_id], season_id: params[:season_id]} , class: "btn btn-success btn-xs"
It's better not to use resources plus path when you need to nest a resource. It's more easy to read and understand, less error prone and it's the Rails convention.
You can write your routes like this:
resources :seasons do
resources :students do
resources :scores
end
end
And then use the named path method in your link like this:
link_to "Edit", edit_score_student_season_path(score, params[:student_id], params[:season_id]), class: "btn btn-success btn-xs"
Related
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
I'm trying shallow nested resources for the first time and having a little trouble with one my index routes.
routes.rb
resources :sites, shallow: true do
resources :visits
end
The error I get is in my visits#show page's back button:
<%= link_to 'Back', site_visits_path(#site) %>
No route matches {:action=>"index", :controller=>"visits", :site_id=>nil} missing required keys: [:site_id]
In the index action of my VisitsController I set#site as follows:
#site = Site.find(params[:site_id])
However it's saying my :site_id is nil and I'm not sure how to set this correctly.
You can set that like:
<%= link_to 'Back', site_visits_path(:site_id => #site.id) %>
I created a nested resources but my link_to shows undefined method 'model_name' for Parking::ActiveRecord_Relation:Class in the browser. I did it wrongly, obviously. How can I correct it?
index.rb
<%= link_to 'Create new parking', new_parking_path %></br>
<%= link_to 'Rent place', [#parking, #place_rent]%>
routes.rb
resources :parkings do
resources :place_rents, :only => [:new, :create]
end
Either #parking or #place_rent is a relation (collection) and not a particular model which you would need to build a route. Depending on what is behind these variables you might need a #first or whole different query.
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)
I am implementing an admin subdomain and have googled to try and find the answer to this, however I have not found another instance.
My routes look like this for the subdomain section:
constraints :subdomain => 'admin' do
scope :module => "admin" do
resources :news, :events
match 'news', :to => 'news#index', :as => 'news'
root :to => "dashboard#index"
end
end
Events works fine, but for some reason in order for news to work I need to add a specific route to match it. It may help to show the partial where the error is generated (admin/shared/menu):
<ul>
<li><%= link_to 'Home', root_path, :class => "#{current_class?(root_path)}" %></li>
<li><%= link_to 'News', news_path, :class => "#{current_class?(news_path)}" %></li>
<li><%= link_to 'Events', events_path, :class => "#{current_class?(events_path)}" %></li>
<div class="clearboth"></div>
</ul>
And then the error if I was to remove the match route:
No route matches {:action=>"show", :controller=>"admin/news"} missing required keys: [:id]
I just don't have a clue why, any thoughts?
I found that this is an issue with rails in that the naming convention news should not be used due to plural issues, news -> new so therefore I had to rename everything to articles instead. Oversight on my part, a tad stupid.
The reason Rails gets confused is because "news" is used for both the singular and plural. news'.singularize gives news; and news.pluralize also gives news (Wikipedia has a longer description of this phenomenon; there are other words that do the same).
Rails will generate a news_path route (plural, for the index action) and a news_path route (singular, for the show action).
The singular route expects a News instance; it appears that the show action is defined later, and overwrites the index action, resulting in the strange behaviour.
The solution is simple: use the news_index_path if you want the index action. I haven't encountered any other issues, and am still using the News model.
ActiveAdmin fixed this issue in the same way.
If you really don't want to use News, then you can suffix it with Item, Entry, Object, or a similar word. This will leave you with a NewsItem model with news_item_path and news_items_path.