I am just beginning to learn how to program and, as practice, am trying to put together a generic facebook-like program. However, when I put in the code provided below, I keep getting the following error:
No route matches {:action=>"new", :controller=>"comments", :post_id=>nil, :user_id=>"4"} missing required keys: [:post_id]
Route
resources :users do
resources :posts do
resources :comments
end
end
View
<% #user.posts.each do |p| %>
<ul><b><%= p.id%></b></ul>
<ul><b><%= p.post%></b></ul>
<ul><em><%= p.created_at%></em></ul>
<ul><%= link_to "Delete Post", [p.user, p], method: :delete %> </ul>
<ul><%= link_to "Comment", new_user_post_comment_path(#user, p.id) %> </ul>
<%end%>
Controller
def show
#user = User.find(params[:user_id])
#post = Post.find(params[:post_id)]
#comment = Comment.find(params[:id])
redirect_to user_path(#user)
end
I've tried a number of different things in the link_to section with the "p.id"; which I think is probably giving me the issue; however, nothing seems to work. I've tried a number of other things that I've read about how to deal with linking with triple nested routes; however, nothing has worked so far.
What am I doing wrong here?
I guess, among all user's comments, there is a comment with post_id = nil. Check all #user.comments for post_id
Your error is this:
No route matches {:action=>"new", :controller=>"comments",
:post_id=>nil, :user_id=>"4"} missing required keys: [:post_id]
The error means you're not sending the post_id parameter, which is required for nested routes. The whole point of a nested route is that you get various resources for parent resources. This means you cannot find the child resources without specifying the parent ones
To fix this, I would recommend the following:
<%= link_to "Delete Post", user_post_path(p.user, p), method: :delete %>
<%= link_to "Comment", new_user_post_comment_path(#user, p) %>
The tricky part is I don't know which of your links is causing the error
You also have a typo in your controller: Post.find(params[:post_id])
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 think it is a routing issue where the controller is not matched properly. I am confused because there is clearly a new action in my ComicTitlesController.
Here is the error I receive when I load the home page, which has the new_user_comic_title_path in the navbar:
Routing Error
No route matches {:action=>"new", :controller=>"comic_titles"}
Try running rake routes for more information on available routes.
In my views:
<li><%= link_to 'Publish' , new_user_comic_title_path %></li>
The ComicTitles controller:
def new
#user = current_user
#comic_title = #user.comic_titles.new
end
Note that ComicTitle is nested under User. Here is the route file:
resources :users, shallow: true do
resources :comic_titles
end
When I run rake routes:
user_comic_titles GET /users/:user_id/comic_titles(.:format) comic_titles#index
POST /users/:user_id/comic_titles(.:format) comic_titles#create
new_user_comic_title GET /users/:user_id/comic_titles/new(.:format) comic_titles#new
edit_comic_title GET /comic_titles/:id/edit(.:format) comic_titles#edit
comic_title GET /comic_titles/:id(.:format) comic_titles#show
PUT /comic_titles/:id(.:format) comic_titles#update
DELETE /comic_titles/:id(.:format) comic_titles#destroy
As you can see the route requires parameter :user_id and your link_to is missing that parameter.
Try updating your link_to definition to following:
<li><%= link_to 'Publish' , new_user_comic_title_path(#user) %></li>
Since you have a nested route, I believe the route is expecting a user_id parameter.
In your link_to helper try adding the following:
<li><%= link_to 'Publish' , new_user_comic_title_path(user_id: current_user.id) %></li>
routes.rb:
resources :shops
shop_controller.rb:
def new
#shop=Shop.new
end
new.html.erb:
<%= form_for(#shop) do |f| %>
....
<% end %>
error:
undefined method `shops_path' for:
<%= form_for(#shop) do |f| %>
The problem is that I already specify the shop resources in the routes file.
Why still get such kind of error?
Any help will be appreciated, thanks
You should use ShopsController not ShopController due to Rails naming convention.
Make sure you have these lines in your rake routes output:
shops GET /shops(.:format {:action=>"index", :controller=>"shops"}
POST /shops(.:format) {:action=>"create", :controller=>"shops"}
OR
shops POST /shops(.:format) {:action=>"create", :controller=>"shops"}
If they aren't present, look carefully at your routes.rb for possible with_options, scope or any other scoping that can affect your resources :shops in such a way that it doesn't generate default url helpers.
since u havent specified the method in the form tag, i guess it is going as a GET request. Try adding the method to ur form
<%= form_for(#shop), :method => :post do |f| %>
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.