Undefined method in controller (Routing) - ruby-on-rails

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.

Related

routes as: path variable not working

I have the following route
resources :projects do
collection do
get 'svdbf', to: 'projects#new', as: 'otherstuff'
end
end
and I am trying to make this link work on the view
<%= link_to 'Other Stuff', otherstuff_url %>
But it's returning the following rails error:
undefined local variable or method `otherstuff_url'
What am I doing wrong?
Based on the rake routes output you posted, a working path would be
<%= link_to 'Other Stuff', otherstuff_projects_path %>
Please run rake routes from the console. That will give you a full list of routes as well as a list of helper functions available to you.
Also try otherstuff_path instead og otherstuff_url

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| %>

undefined method `protect_against_forgery?' for #<#<Class:0x0

I have the following code in my routes.rb file .
resources :users do
member do
get :following,:followers
end
collection do
put :activate_email
end
end
And I have a user email activation link like this :
<%= link_to "Activate",activate_email_users_url(email_token: #user.email_token),method: :put %>
When I click on the activate link , this is the url that is generated
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
Update: Ok, So I think I kno what the problem is . When I look at the html source of the activation email in my gmail which contains the link_to , there is no data-method='put'. So that seems to be the problem . It is always sending a default GET request instead of PUT.
This is my user_mailer/registration_confirmation.html.erb file
<%= javascript_include_tag "application" %>
</head>
Please click on the following link to activate your email
<%= link_to "Activate",activate_email_users_url(email_token: #user.email_token), method: :put %>
This gives the following error :
undefined method `protect_against_forgery?' for #
So , the code <%= javascript_include_tag "application" %> is causing this error. Is there any way around this ?
Sorry, I do not know your purpose, but apparently you have a purpose to activate user.
Try this, if this solution not work, please tell me your action (activate_email) on controller!
see on rake routes output :
activate_email_users PUT /users/activate_email(.:format) users#activate_email
user GET /users/:id(.:format) users#show
when your generate
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
Your problem was activate_email considered to be :id
users/activate_email => users/:id
And solution for your problem :
Try removing the method from the link. Its better specifying the method in your routes file. How about replacing match by put in routes as :
resources :users do
member do
get :following,:followers
end
end
put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate"
and on view
<%= link_to "Activate", activate_path(:email_token => #user.email_token) %>
I have not tested this, but I guess this will suffice.
UPDATE
for Question : undefined method `protect_against_forgery?'
Add this to a helper that only your mailer template uses:
def protect_against_forgery?
false
end
NOTE : If You have new question, please create new "Ask Question" and aprrove answer is usefull for this question
If you're trying to activate a single user account you probably don't want to be specifying your route on the collection (which you would use for actions that operate on multiple users).
Here's some (untested) code that should point you in the right direction:
controller :users do
put '/activate/:email_token', :to => :activate, :as => 'activate_email'
end
Which should route a PUT to /activate/xxxx to the UsersController#activate action with a params[:email_token] set as xxxx. It should also give you a #activate_email_url route which you can pass the activation token (you can check what routes your app provides by running rake routes on the command line).
Google redirected me to this question even-though mine was related to rendering a template into a string and not just in the browser. My solution for the template problem was something along these lines:
action_controller = ActionController::Base.new()
action_controller.class_eval do
def protect_against_forgery?
false
end
end
file_string = action_controller.render_to_string('/some_template/template_file',locals: { local_variable: 1 }

path defined in routes file is not found

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.

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