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
Related
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.
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.
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])
My attempt to make a new-action which gives the user a form and a create-action to process it fails with the error message `.
View: app/views/studios/new.html.erb:
<%= form_for #studio, url: {action: 'create'} do |f| %>
<%= f.text_field :name %>
<%= f.submit 'Create' %>
<% end %>
Controller: app/controllers/studio_controller.rb:
def new
#studio = Studio.new
respond_to do |format|
format.html
format.json { render json: #studio }
end
end
def create
# TODO
end
Route: config/routes.rb
get 'studios/new' => 'studios#new', :as => 'new_studio'
On attempting to visit http://localhost:3000/studios/new, I am presented with the error
No route matches {:action=>"create", :controller=>"studios"}
As you can see, the create-action is present in the studios-controller. Why is it failing?
It's failing because you don't have route defined for the create action. You have a get action defined for the new action only.
Update your routes file to add a post route to the create action as:
post 'studios/create' => 'studios#create'
Or, you could choose to use resourceful routing and update your routes file as:
resources :studios, only: [ :new, :create ]
This will define the new and create route for your studio resource. To see the generated routes you can run rake routes.
The rails form helper, given the instance variable #studio, asks for the instance variable via the new action, and, upon clicking save, calls the create action to validate and save this new instance. It's failing because rails cannot find the action to submit the object to.
Try adding resources :studios for RESTful routes in config/routes.rb (index, new, create, show, edit, update, destroy) automagically routing to actions with the same name in the app/controllers/studio_controller.rb
Try visiting the docs for form helpers here: http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object for a more in depth explaination.
I want to pass a parameter to the index action, but the I'm only getting the show action.
routes.rb:
Test1::Application.routes.draw do
resources :blog
end
blog_controller.rb:
def show
# code
end
def index
# code
end
View url that send to show action instead to index action:
My link
What should I add in routes file or in view?
Output of my routes:
$ rake routes
blog GET /blog(.:format) {:action=>"index", :controller=>"blog"}
blog GET /blog/:id(.:format) {:action=>"show", :controller=>"blog"}
The command line will show you routes you can use with rake routes
The route you want is blogs_path and you can add a parameter on to that, e.g. blogs_path(other_item => :value).
Exactly how will depend on whether you are try to use it in a controller, another view, etc.
For the view have: <%= link_to 'My Link', blogs_path(:other_item => value) %>
It sounds like you want 2 routes:
/blogs/:other_param
/blogs/:id
But, for as smart as Rails is, it can't figure out whether the param is meant to be treated as an other_param or as an id.
So the simplest solution is to add this route to the resources defaults like so:
resources :blogs
get "/blogs/other_param/:other_param", to: "blogs#index", as: "other_param_blogs"
That way Rails knows that if you're going to /blogs/other_param/current, then it will treat current as the :other_param.
Use below code to pass parameter:
My link
or
<%= link_to "My link", blog_path(name: "test") %>
above code will redirect to index action with name as key and test as parameter,