uploading and import csv to rails - ruby-on-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

Related

How can i find the path helper from specific route i have

I have a specific path helper that works perfectly in rails 2.3 but throws error on rails 3.1.
Here is the path helper.
shipping_price_store_return_path(store)
When i use this in rails 3.1 it gives me error saying
NoMethodError: undefined method `shipping_price_store_return_path' for #<ActionDispatch::Integration::Session:0x007fb2da730228>
when i run rake routes This is whati get shipping_price_store_return_index /stores/:store_id/return/shipping_price(.:format) {:action=>"shipping_price", :controller=>"return"}
can anyone suggest what could be going wrong here.
below are the content of routes file
resources :stores do
resources :return do
match :shipping_price, :on => :collection
end
end
As your resource name is :return instead of :returns that Rails decided to add the _index to any collection nested underneath. This change has been done from rails 3 onwards.
So the new rails 3 route should be:
shipping_price_store_return_index_path
If you want to avoid the _index then either you can use resources :returns or you can make it resource :return.

path helper generating exeception on console while migrating from Rails-2 to Rails-3

I am trying to modify my script, i am migrating from rails 2.3 to rails 3.1 but i am facing a strange issue. I see that when i use a path helper like this
in rails 3.1 i am getting an exception but rails 2.3 it used to work, by work i mean when to the path helper i pass values and order.customer_id is nil, it generates a path to create new customer however in rails 3.1 i see it generates a exception, below is the description of the error i see in rails 3.1
helper.link_to customer_email, app.store_customer_path(store,order.customer_id) , when order.customer_id is blank i get a exception on console as below
Below is the error i observe in rails 2.3
ActionController::RoutingError: store_customer_url failed to generate from {:controller=>"customers", :action=>"show", :store_id=#<object>}
. but when i load a web page i see that i get a path generated to create a new customer.
here is my relevant routes.rb code
resources :stores do
resources :customers do
collection do
get :get_customers, :download, :csv_template
end
match :upload, :import, :map, :on => :collection
member do
get :more
end
resources :dropship_profiles
resources :address
end
end
But in rails 3.1
on console to i see exception and when loaded from browser also i see exception
I am not able to understand this and its confusing me, can anyone please help, Thanks.
After going through your routes.rb,i dont see any relevant path that routes to store_customer_path...kindly use the one present or create new one...verify by running rake routes > path.txt and then check your routes easily in path.txt
=============UPDATED ANSWER==============
here you are passing two values,you just need to pass one
Instead of helper.link_to customer_email, app.store_customer_path(store,order.customer_id)
try
helper.link_to customer_email, app.store_customer_path(order.customer_id)
or
helper.link_to customer_email, app.store_customer_path(store.id)
As your path says /stores/:store_id/customers/new(.:format),you just need to pass store_id for it to work.
for understanding major changes in Routes from Rails 2 to Rails 3...you must have a look at this page

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

autogenerate paths in rails 3?

From the looks of some railscasts (this one in particular), it seems like there is some autogeneration of "*_path" variables that not happening for me. in this rails cast, the edit_mutliple_products_path seems to be generated automagically (i don't normally like using that word). When I follow through the same steps and try to access a similar path, i get this:
undefined local variable or method `edit_multiple_distributions_workflows_path' for #<#<Class:0x132b18a68>:0x132af3290>
This is rails 2.X. Rails routes changed in Rails 3. in order to get this route add below to routes.rb:
resources :products do
collection do
post 'edit_multiple'
put 'update_multiple'
end
end
You will be able to access this paths with
edit_multiple_products_url
edit_multiple_products_path
update_multiple_products_url
update_multiple_products_path
instead of edit_multiple_distributions_workflow_path. Btw where did you get this path from? I did not see it in the railscast.
In the given tutorial, which looks like it's from an older Rails, this is the line which would generate the path methods:
map.resources :products, :collection => { :edit_multiple => :post, :update_multiple => :put }
In rails 3, you can see its usage in the docs here: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Recognize routes in rails console Session

Say I have a router helper that I want more info on, like blogs_path, how do I find out the map statements behind that in console.
I tried generate and recognize and I got unrecognized method error, even after I did require 'config/routes.rb'
There is a good summary with examples at Zobie's Blog showing how to manually check URL-to-controller/action mapping and the converse. For example, start with
r = Rails.application.routes
to access the routes object (Zobie's page, a couple years old, says to use ActionController::Routing::Routes, but that's now deprecated in favor of Rails.application.routes). You can then check the routing based on a URL:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
and see what URL is generated for a given controller/action/parameters combination:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
Thanks, Zobie!
In the console of a Rails 3.2 app:
# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
# use routes normally
users_path #=> "/users"
Basically(if I understood your question right) it boils down to including the UrlWriter Module:
include ActionController::UrlWriter
root_path
=> "/"
Or you can prepend app to the calls in the console e.g.:
ruby-1.9.2-p136 :002 > app.root_path
=> "/"
(This is all Rails v. 3.0.3)
running the routes command from your project directory will display your routing:
rake routes
is this what you had in mind?
If you are seeing errors like
ActionController::RoutingError: No route matches
Where it should be working, you may be using a rails gem or engine that does something like Spree does where it prepends routes, you may need to do something else to view routes in console.
In spree's case, this is in the routes file
Spree::Core::Engine.routes.prepend do
...
end
And to work like #mike-blythe suggests, you would then do this before generate or recognize_path.
r = Spree::Core::Engine.routes

Resources