I want to route requests something like this: reports/bloodtypes is routed to controller reports, action bloodtypes, with format = pdf, and the route named as bloodtype_report. The Guides gives an example
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
When I do this:
match 'reports/bloodtypes' => 'reports#bloodtypes', :defaults => {:format => 'pdf'}, :as => 'bloodtype_report'
or this
match 'reports/bloodtypes' => 'reports#bloodtypes', :format => 'pdf', :as => 'bloodtype_report'
the controller still does not receive the :format => 'pdf' in params, and tries to render the report as HTML. The funny thing is that the route is shown by Rake as
bloodtype_report : /reports/bloodtypes(.:format) : {:format=>"pdf", :controller=>"reports", :action=>"bloodtypes"}
whether I use the first form (with :default) or second (just setting the format to pdf). It seems the route is correct, so why is the format parameter not being passed to the controller?
have you tried adding this to your controller:
respond_to do |format|
format.html
format.pdf { render :pdf => "show" }
end
Related
I have an app that I am working on that directs users to a "bundle" page when purchasing one product so that they have the opportunity to add another product to "bundle" their purchase for a discount.
Here is my routes:
resources :orders, :path_names => { :new => 'checkout' }
match "/orders/bundle" => "orders#bundle", :as => 'bundle_order'
match "/orders/add_product" => "orders#add_product", :as => 'add_product'
Here is my Controller#Action
def bundle
op_client = Client.find_by_name(opposite_client(current_client))
#product = Product.find_by_client_id_and_type_and_status(op_client.id, "subscription", "Active")
respond_with #product
end
For some reason when I redirect_to this method, I receive this error:
Unknown action
The action 'show' could not be found for OrdersController
I don't have a show method in my OrdersController cause I don't need it. Why would I be seeing this issue?
This error could be caused by two diferent things.
first:
are you using something like: <%= link_to #order_object %> ?? if you are, this is the problem.
second:
on routes.rb change this line:
resources :orders, :path_names => { :new => 'checkout' }
to
resources :orders, :path_names => { :new => 'checkout' }, :except => [:show]
this should work. if not, please give more details about the code you are using to do the redirect_to
On a rails 3 app, I've got these routes defined:
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :sign_out => 'logout' } do
get 'login' =>'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'registrations#new', :as => :new_user_registration
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
# catchall route to deal with routing errors
match "*path" => "error#index"
However, now when I go to log /user/auth/facebook, it routes me to the errors page...
My solution to this was to add constraints to the final match:
match "*path" => "error#index", :constraints => lambda {|req| !req.path.starts_with?("/users/auth/") }
This works, but I am wondering if there is a better way... ?
I think you are better off letting a 404 page handle routing errors. You only get a 500 error in development. It's a 404 in production. For example on my site: http://agmprojects.com/test. So i suggest using the 404.html in the public folder instead, rather than loading that controller. Out of curiosity, are you responding with a 404 http status code?
I have in my haml:
= link_to("Calls Today", todo_path)
And in my routes.rb:
match "todo/today" => "todo#show_date"
match "todo/today/campaign/:id" => "todo#show_date", :as => "todo"
My understanding is that 'todo_path' should find todo controller and show_date.
This route :
match "todo/today/campaign/:id" => "todo#show_date", :as => "todo"
Expects an id parameter. Therefore, your link_to should be like :
= link_to("Calls Today", todo_path(:id => your_id))
I have a form that is suppose to be a POST to CREATE action but it is doing a POST to INDEX action. So i then decided to test my routes with rspec. In my Example i have my test as follows.
it "should recognize a specific invoices#create route" do
assert_routing("/invoices", {:controller => "invoices", :action => "create"})
end
but when i run the test its coming up with this error.
1) InvoicesController on get to :index should recognize a specific invoices#create route
Failure/Error: assert_routing("/invoices", {:controller => "invoices", :action => "create"})
The recognized options <{"action"=>"index", "controller"=>"invoices"}> did not match <{"controller"=>"invoices", "action"=>"create"}>, difference: <{"action"=>"create"}>.
Expected block to return true value.
So im trying to figure out why my form is doing a POST on INDEX and why my test thinks im doing an index route. I have tried inserting :method => :post in the test but it doesnt seem to work.
Have you tried this?:
assert_routing({ :path => "invoices", :method => :post },
{ :controller => "invoices", :action => "create" })
<%= will_paginate #semails, :renderer => 'RemoteLinkRenderer' , :remote => {
:loading => 'loadingPanel.show()',:complete => 'loadingPanel.hide()'} %>
in rails2
how to convert this to rails3
This is the routes using for rails 2 for semails
map.resources :users,
:collection => {:uapload_avatars => :post, :aselect_friend => :get, :alist_friend => :get, :aist_moderator => :get},
:member => {:anew_avatars => :get, :acreate_avatar => :post
} do |user|
user.resources :semails, :collection => { :sort => :get, :asave_draft => :post }
end
how to convert this routes in rails 3 ?
I'm facing this error
will clicking the pagination link it just redirecting to the home page is that routes issue or will paginate issue in rails 3
please help me to solve this issue
I'm not 100% about the spelling but the new routes for rails would approximate something like this:
resources :user do
collection do
post "uapload_avatars"
get "aselect_friend"
end
resources :semails do
collection do
get "sort"
post "asave_draft"
end
end
end
http://guides.rubyonrails.org/routing.html