URL Helper - undefined method - ruby-on-rails

rake routes:
api_v1_accounts GET /api/v1/accounts(.:format) {:action=>"index", :controller=>"api/v1/accounts"}
That means I should have a url helper method:
api_v1_accounts_url
I am trying to test creating an account with rspec and the following line fails:
route = api_v1_accounts_url(account)
Saying that api_v1_accounts_url is an undefined method
I guess I've really misunderstood something about URL helper methods and the scope of them. Please help

include Rails.application.routes.url_helpers
That did the trick

Related

Rails - How To Get Full URL in Model

In my Rails 5 app, I'm trying to get the full URL for my instance, but when I tried to use url_for as suggested in this SO question, I get an error, NoMethodError: undefined method 'url_for' for #<Product:0x8646fc0>.
Full URL with url_for in Rails
def get_url
url = url_for(self)
end
I also tried product_url(self) and received a similar error.
What's the proper way to get the URL for my instance? Thanks!
helper method is only available in view or decorator.
it's better to define url in the decorator.
if you want to use helper method in the model, try this
def url
Rails.application.routes.url_helpers.product_url(self)
end

Rails get routing with parameter

I want to create a new route in my routes.rb which points to a "courses" controller which has the method pdfdownload. The route is supposed to take 2 parameters: course_id and user_id. I thought it should be like this:
get "/courses/pdfdownload/:course_id/:user_id"
The courses controller and everything works fine until I add the line above. The courses controller has a method called pdfdownload. Nevertheless, when I try to start the server (rails s), I get the following error:
warning: already initialized constant Mime::PDF
warning: previous definition of PDF was here
Exiting
`default_controller_and_action': missing :controller (ArgumentError)
When I type rake:routes I get:
missing :controller
The courses controller is existing and is working very well with many methods. After I altered the line to this:
get "/courses/pdfdownload"
The server starts.
The rails guides says at "3.2 Dynamic Segments", it has to be written this way:
get ':controller/:action/:id/:user_id'
Whats wrong here? Thank you very much!
Update: I'm using the following link in the view:
<%= link_to "PDF", courses_pdfdownload_path(#course.id, user.id) %>
Please have a try
get "/courses/pdfdownload/:course_id/:user_id" => "courses#pdfdownload", :as => "courses_pdfdownload"
try match "/courses/pdfdownload/:course_id/:user_id" => "courses#pdfdownload"
The correct route would be:
get '/courses/pdfdownload/:course_id/:user_id', to: 'courses#pdfdownload'
But for a nicer REST route, I would rather change it to this:
get '/courses/pdfdownload/:id/:user_id', to: 'courses#pdfdownload'
The fact that the action deals with a Course resource is already implied by the name of the controller handling the action. So you don't need to call the Course id :course_id, simply :id is enough.
Edit
Note also that you can customize the name of the route helper like this:
get '/courses/pdfdownload/:id/:user_id', to: 'courses#pdfdownload', as: 'courses_pdfdownload'
Your route helper will then be courses_pdfdownload_path.
As for the errors,
warning: already initialized constant Mime::PDF
warning: previous definition of PDF was here
this is due to Rails registering PDF by default since 2011. No need to register them in config anymore.
https://github.com/rails/rails/commit/d73269ba53992d8a01e5721aad3d23bc2b11dc4f

uploading and import csv to rails

i have followed this tut enter link description here, though i seem to have encountered a few issues. the problem i am getting is
NameError
undefined local variable or method `map' for #<ActionDispatch::Routing::Mapper:0x007f81b1bd0170>
which i believe is related to the routes.rb
map.resources :imports
map.import_proc '/import/proc/:id', :controller => "imports", :action => "proc_csv"
im using Ruby 1.9.3, Rails 3.2.3
map is the keyword used for routing in Rails 2. Rails 3 routing is substantially changed. You want something more like this:
resources :imports do
member do
get :import_proc
end
end
For more information, check out the Rails routing guide.
import_proc is a member method so you need to pass in a parameter
import_proc_path(id)
Member methods require a parameter, an ID
Collection methods does not require a parameter so it doesn't require a parameter

Rails route - undefined local variable or method

Just getting started with Rails and I am having a problem defining a route. None of the documentation I can seems to resolve the issue and I am getting an undefined local variable or method error.
I need to click on a link and take a specific action, sortit. sortit does not have a view because it sorts mystuff objects and then redirects to the index page. When I use one of the predefined actions then in fact everything works. Of course none of the predefined actions are what I want to do.
my /config/routes.rb file:
match "/mystuff/sortit'", :controller => "mystuff", :action => "sortit"
resources :mystuff
my /app/controllers/mystuff_controller.rb file
class MystuffController < ApplicationController
....
def sortit
#mystuff.sort
redirect_to_mystuff_path
end
....
end
my /app/views/mystuff/index.html.haml file:
-# This file is app/views/mystuff/index.html.haml
%h1 All My Stuff
%table#mystuff
%thead
%tr
%th= link_to raw("Type"), sortit
....
As I said, when I replace sortit with a predefined action, then that action is executed. However sortit fails with this error:
undefined local variable or method `sortit' for #<#<Class:0x9997a10>:0x997c0f8>
app/views/mystuff/index.html.haml:7:in `_app_views_mystuff_index_html_haml__61272557_87671610'
So what have I missed and how do I get sortit to execute when clicking on the generated link for Type?
Thanks!
PS: My rake routes output:
mystuff GET /mystuff (.:format) {:action=>"index", :controller=>"mystuff"}
....
mystuff_sortit /mystuff/sortit (.:format) {:controller=>"mystuff", :action=>"sortit"}
....
So rake routes shows my route with the name mystuff_sorit - but aren't actions prepended to the object name? Shouldn't it be sortit_mystuff instead?
Your action is sortit but your view is index
Change your action to index
redirect_to needs a space also, so use redirect_to mystuff_path...
which will actually be my_stuffs_path (plural) which you can see from rake routes (at the command line, most useful) which you get from defining it as a resource in routes.

Namespaced routes

I must be missing something with this rather trivial routes implementation in Rails 3.
I have a namespaced route like so:
namespace 'dashboard' do
get 'download', to: "Index#download"
end
If I run rake routes I see:
dashboard_download GET /dashboard/download(.:format) {:action=>"download", :controller=>"dashboard/Index"}
The URL is super, that's exactly what I want (and will have many more matches in the namespace), but the controller is wrong. It should just be Index, not dashboard/Index.
Is there any way of fixing this?
Or is that the wrong way to implement that style of route?
Cheers.
To remove the module prefix do this:
scope '/dashboard' do
get 'download', to: "Index#download"
end
You can find more information and alternatives here.

Resources