path defined in routes file is not found - ruby-on-rails

ORIGINAL POST:
I'm probably defining my route incorrectly but I'm following along with this example.
I've defined my routes like this:
namespace :admin do
namespace :distributions do
resources :workflows do
collection do
post :edit_multiple
put :update_multiple
end
end
end
end
obviously there are a bunch of other things within in the admin namespace and I wanted my distributions to belong to the admin namespace and workflows inside of that.
however when I do this:
<%= form_tag admin_edit_multiple_distributions_workflows_path do %>
or this:
<%= form_tag admin_distributions_edit_multiple_workflows_path do %>
I get an undefined method or variable error:
undefined local variable or method `admin_distributions_edit_multiple_workflows_path' for #<#<Class:0x12c2b2320>:0x12c29dfd8>
what'd I do wrong?

You have to put edit_multiple or update_multiple in front, like so:
edit_multiple_admin_distributions_workflows_path
update_multiple_admin_distributions_workflows_path
But, as Nick already mentioned in his comment, rake routes should give you the answer anyway.

Related

Undefined method in controller (Routing)

I'm new at rails and I'm currently working on an already existing application that handles butons like so:
<%= link_to 'Edit', edit_answer_path(ans) %>
That links to the file /answers/edit.html.erb but now I need to make a button that links to the file /answers/comment.html.erb how do I go about doing this?
I already tried with
<%= link_to 'Comment', comment_answer_path(ans) %>
but I get the error "Undefined method 'comment_answer_path'" even after adding this lines to answers_controller :
def comment
ans = Answer.find(params[:id])
end
You need to add a route to your config/routes.rb and then restart the server. Something like
resources :answers do
member do
get 'comment'
end
end
will create the comment_answer_path helper for you as well.
It depends on how you've set up the routes in routes.rb.
You can use rake routes to see the list of all paths and their alias.

Overwriting rails URL helper (form_for)

I am trying to override a url helper that is being called from a form_for.
My controllers and models are all namespaced in the "Admin" module, and are resting in an admin folder. (So models/admin/foo.rb, controllers/admin/foo_controller.rb)
My routes are correct (I believe)
scope module: "admin" do
resources :foos do
resources :foo_details
end
end
Here is some code to demonstrate what I am attempting to do and where I get the error:
<%= form_for(#foo) do |f| %>
I am getting an error on the form for line, with this: "undefined method `admin_foos_path' for #<#:0x007f08fd7ffb90>.
When I run 'rake routes', I have a route for foos, but not admin_foos. I assume the form_for is trying to access admin_foos_path because of how the application was scaffolded.
Is there a way for me to override admin_foos_path to be foos_path or redirect it? I have tried searching for this issue and haven't been able to come up with much.
Thanks in advance.
Use the namespace directive
namespace :admin do
resources :foos do
resources :foo_details
end
end
Using scope just gives you foos_path, while namespace gives admin_foos_path which is needed for the #foo to work.
undefined method `admin_foos_path' for
To explain the error, #foo is a model instance of Admin::Foo not Foo, so form_for by default look for its resourceful route which in your case it is admin_foos. And because it can't find it, so is the error.
Is there a way for me to override admin_foos_path to be foos_path or
redirect it?
You can overwrite the URL of a form to your desired URL like below
<%= form_for(#foo, url: foos_path) do |f| %>
But I believe you need namespace instead of scope. I recommend to read about when to use scope and namespace in controller-namespaces-and-routing

url helper for namespace nested model

I am using the public_activity gem. To use the gem, you do not need to create an activities controller for it. So I did not. However, I do have a comments controller. I want to have the following url helper for the comment's create action:
public_activity_activity_comments_path(#activity)
I have tried two things in my routes and both have failed. First, I tried using a namespace route:
namespace :public_activity do
resources :activity do
resources :comments
end
end
This produces the error:
ActionController::RoutingError - uninitialized constant PublicActivity::CommentsController:
Since I do not have a PublicActivity::Activities controller, I tried this instead:
get '/public_activity/activity/:activity_id/comments/new', to: 'comments#new'
post '/public_activity/activity/:activity_id/comments', to: 'comments#create'
However, this does not seem to produce any url helpers at all, as it gives me the following error:
NoMethodError - undefined method `public_activity_activity_comments_path' for #<#<Class:0x007f868e1b7760>:0x007f868e1afd58>:
The reason why I want a route like that is because in my comments view, the commentable can be any model, so I prefer to have a polymorphic form:
<%= form_for [commentable, Comment.new] do |f| %>
...
Am I out of luck? Will I have to hardcore the url for form_for?
IMHO, you only need to play with your polymorphic association and form, Public_Activity will record the activities if you correctly configure that any model of yours.

Routing Issue for Model that Rails Can't Determine Word Roots

I have a model named Faq and it looks like Rails is having a hard time with the dynamic paths generated from resources :faq.
Here is what rake routes puts out.
admin_faq_index GET /admin/faq(.:format) admin/faq#index
POST /admin/faq(.:format) admin/faq#create
new_admin_faq GET /admin/faq/new(.:format) admin/faq#new
edit_admin_faq GET /admin/faq/:id/edit(.:format) admin/faq#edit
admin_faq GET /admin/faq/:id(.:format) admin/faq#show
PUT /admin/faq/:id(.:format) admin/faq#update
DELETE /admin/faq/:id(.:format) admin/faq#destroy
The problem is when I use form_for like so:
<%= form_for([:admin, #faq]) do |f| %>
I get this error:
undefined method `admin_faqs_path' for #<#<Class:0x007fdda4627a58>:0x007fdda41a5098>
Try changing routes.rb to
resources :faqs
Then verify that #faq is not nil.
Based on the fact that you're using a form, I'm guessing your FAQ is not a singular resource.
You just have to add an excpetion in config/initializers/inflector.rb
like this one
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w(faq)
end
It add an exception to pluralize and inflector rules of rails
As you have mentioned in routes resources :faq , it has not generate the routes admin_faqs_path but in the form_for by default it is searching for the admin_faqs_path. So we need to override it. Please change the code to
<%= form_for([:admin, #faq], :url => admin_faq_index_path, :method => :post) do |f| %>

Making a controller exist on as a sub-controller, how to fix form routes in rails

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 :)

Resources