I seem to have a problem that I can't find the solution for myself, I hope someone can help.
I have a form defined like so:
<% form_for #leads do |f| %>
I have a resource called #leads (map.resource :leads)
But when I look in the HTML code of the page it generates, I see as a form action the following
<form action="/lead.%23%3Clead:0x10333e858%3E" class="edit_lead" ... etc
The lead.%23%3Clead:0x10333e858%3E as a form action does work, however rails doesn't know what to do with it after it updates. Does anyone know how I can make this a normal URL so that rails can redirect after the update again?
Thank you very much
Regards, Marco
I think you have to rename your route from
map.resource :leads
to
map.resources :leads
because you have multiple leads (and not only one -> so no "resource", its "resources")
If you are using a singular resource you should not pass the object to the url helper, ie. lead_path not lead_path(#lead).
However it does look like a typo and your route should be map.resources :leads
Related
I am having issues with the link_to method in Rails. I have the routes established, but the urls aren't working correctly.
3000/gov_official => my root page
3000/gov_official/1 => desired show page url
3000/gov_official.1 => what I am getting...
Any help would be greatly appreciated.
My code snippet:
Try the following in your routes instead of manually defining the index/show routes.
resource: :gov_official, only: [:get]
Probably having an issue link_to, try this one.
<%= link_to gov.name, gov_official_path(id: gov.id) %>
From my own experience, the problem is that Rails can't figure out which path are you trying to link to. Because, your resource is not in plural form gov_officials, you don't have 2 clearly separate paths:
gov_officials_path - takes 1 argument format. Generates: gov_officials.format.
gov_official_path - takes 1 argument model. Generates: gov_officials/:id
So, to solve your problem the Rails way, use pluralization of your resources correctly.
I have a routes file (Rails 4.0.4 App):
resources :products do
resources :variants, except: :index
end
rake routes gives:
product_variants POST /products/:product_id/variants(.:format) variants#create
new_product_variant GET /products/:product_id/variants/new(.:format) variants#new
edit_product_variant GET /products/:product_id/variants/:id/edit(.:format) variants#edit
product_variant GET /products/:product_id/variants/:id(.:format) variants#show
PATCH /products/:product_id/variants/:id(.:format) variants#update
PUT /products/:product_id/variants/:id(.:format) variants#update
DELETE /products/:product_id/variants/:id(.:format) variants#destroy
So as proposed I'm using
<%= form_for [#product, #variant] do |f| %>
but this generates (note the URL):
<form accept-charset="UTF-8" action="/variants/1" class="edit_variant" id="edit_variant_1" method="post">
And I get (of course) the routing error saying no route matches with PATCH /variants/1
Same strange behaviour when I redirect to in an Api Controller living namespaced under api/:
redirect_to [:api, #product, #variant]
Got me: no route matches with /api/variants/1
So:
I checked all routes
I know how to nest resources and namespace them
I thought I know how to generate the URL's ;-)
Somehow the #products is ignored when generating the urls
Has anyone a clue where to look any further?
Take a look at your variants controller. Are you instantiating #product?
It may be helpful to post the code for controller too.
Hope this helps!
Alex
Okay the problem really lies in the different controllers. I somehow instantiated #products in a before action, but obviously did it wrong.
Seems to be a leck of energy drink problem. thanks for pointing me to the right solution!
I have a ChaptersController that does not have a direct route (i.e. site/chapters/:id) but only exists as a sub route for a BooksController (i.e. site/books/:id/chapters/:id). however, when I try to go to books/:id/chapters/new , I get the following routing error:
Showing .../app/views/chapters/_form.html.erb where line #1 raised:
No route matches {:controller=>"chapters"}
how can I fix this?
It seems like you are using nested routes in this manner:
resources :books do
resources :chapters
end
in which case you should have the named routes 'book_chapter' and 'book_chapters'. You can check this by running rake routes.
In your _form.html.erb partial you need to change this line:
<%= form_for(#chapter) do |f|%>
You need to specify the target URL of the form explicitly, and probably also handle different URLs for create and update scenarios. Try something like this:
<%= form_for(#chapter, :url => (#chapter.new_record? ? book_chapters_path(#book) : book_chapter_path(#book, #chapter) )) do |f| %>
I suppose there is wrong path in /app/views/chapters/_form.html.erb
Check what url is in tag. I suppose you forgot to change it to nested in books.
You may as well paste _form.html.erb here, so i will point it out :)
I have the following route in rails 3:
resources :jobs do
member do
post :seller_job_submitted
end
end
And the following form
=form_for job, :url=>seller_job_submitted_job_path(job), :remote=>true do |f|
I know it's not very restful, but it's kind of a stop gap for now. In any case, I keep getting this error when submitting the form
Started POST "/jobs/74/seller_job_submitted" for 127.0.0.1
ActionController::RoutingError (No route matches "/jobs/74/seller_job_submitted"):
but when I run rake routes | grep seller_job_submitted, I think the correct results come up:
seller_job_submitted_job POST /jobs/:id/seller_job_submitted(.:format) {:action=>"seller_job_submitted", :controller=>"jobs"}
Any ideas about what might be going on?
Thanks!
Assuming you have defined method seller_job_submitted in model and controller.
Replace your code with
resources :jobs
match "jobs/:id/seller_job_submitted" => "jobs#seller_job_submitted", :as => "seller_job_submitted"
Then in form_for tag use :url=>seller_job_submitted_path
This should fix your problem: you did not define seller_job_submitted_job_path explicitly.
Perhaps use put instead of post? Or use :post as the method in the submit form.
You can tell if this is the issue by looking at what the REST method is for the generated form (look for the hidden field in the page source).
So in short, maybe Rails is somehow expecting a POST on that URL but it's receiving a PUT.
Yes, this is a regression bug with Rails 3.
It turns out you need to be careful about using POST in your routes.rb.
resources :jobs do
member do
post :seller_job_submitted # will not work
put :seller_job_submitted # will just work
end
This is even though the FORM method says POST.
I'm having a little issue...I setup a rails application that is to serve a german website. To make use of Rails' internal pluralization features, I kept all my models in english (e.g. the model "JobDescription").
Now, if I call "http://mysite.com/job_descriptions/", I get all my job_descriptions....so far, so good. Because I didn't want the english term "job_descriptions" in my url, I put the following into my routes.rb
map.german_term '/german_term', :controller => 'job_descriptions', :action => 'index'
map.german_term '/german_term/:id', :controller => 'job_descriptions', :action => 'show'
If I call "http://mysite.com/german_term/" or "http://mysite.com/german_term/283" I get all my job_descriptions, which is fine.
However, to make the URL more SEO friendly, I'd like to exchange the id for a more userfriendly slug in the URL. Thus, I put the following in my job_description.rb:
def to_param
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
end
which, whenever I use "job_description_path" in any link_to method, renders my URLs out to something like "http://mysite/job_descriptions/13-my-job-description-title".
However, and this is where I'm stuck, I'd like to get "http://mysite/german_term/13-my-job-description-title". I already tried to exchange the "job_description_path" with "german_term_path" in the link_to code, but that only generates "http://mysite/german_term/13". Obviously, to_param isn't called.
One workaround I found is to build the link with:
<%= link_to job_description.name, german_term_path(job_description.to_param) %>
But that's rather tedious to change all the link_to calls in my code. What I want is to replace "job_description" by "german_term" whenever it occurs in a URL.
Any thoughts?!?
Regards,
Sebastian
I think you're going to need to use the restful route helpers to get what you want.
In that case, it wouldn't take much re-factoring (assuming you've mapped JobDescriptions as a resource). Leave your to_param as is and change your JobDescriptions route to something like the following:
map.resources :job_descriptions, :as => 'german_term'
Hope this helps!
Rails only utilizes the
def to_params
end
URL builder when you are using a restful route/link helper. The only way I am aware of is to do it similar to how you did, unless you are willing to just scrap your english language links and do it all in German. In that case, just get rid of the named route lines and change the to_params to use the correct name field from the database. At that point, the REST routes should behave correctly.