routes as: path variable not working - ruby-on-rails

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

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.

Rails 4 Undefined Local Variable or Method for Download link

I keep getting undefined local variable or method `orders_download_template_path' for #<#:0x0000376c505098>
with the following code. I have also tried download_template_path and #orders_download_template_path (this last one doesn't throw an error, but does nothing either).
Routes.rb:
resources :orders do
collection do
post :import
get :upload_page, as: 'upload_page'
get :search, as: 'search'
get :csv_report, as: 'csv_report'
get :overdue_csv_report, as: 'overdue_csv_report'
get :download_template, as: 'download_template'
end
end
orders_controller.rb:
def download_template
send_file Rails.root.join('public/upload_template.csv'),
type: 'application/csv',
x_senfile: true
end
view:
<%= link_to "Blank Upload Template", orders_download_template_path %>
The File has been Placed under /public
Based on the Rails routing guide, you have a syntax error.
http://guides.rubyonrails.org/routing.html#adding-collection-routes
This should fix it
download_template_orders_path
If you run rake routes on your console, you should see the following line (among others):
download_template_orders GET /orders/download_template(.:format) orders#do
as you can see, if you're setting this route on a COLLECTION, the path generate would be download_template_orders_path.
If you set it on a MEMBER, you would see this:
download_template_order GET /orders/:id/download_template(.:format) orders#
Answering your question, the correct path would be download_template_orders_path

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.

Resources