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
Related
I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?
At each step in my checkout process, an order is updated via a PUT request. However, one of the states has a form that submits to a third party which redirects back to my site, calling the update method with GET (no control over this).
Why does my respond_with code appear to be totally ignored and I get a Missing Template checkout/update error? It should be hitting #edit.
CheckoutController.rb
before_filter :load_order
def update
if #order.update_attributes(params[:order])
#order.next
end
respond_with(#order, :location => checkout_state_url(#order.state))
end
routes.rb
match '/checkout/update/:state' => 'checkout#update', :as => :update_checkout
match '/checkout/:state' => 'checkout#edit', :as => :checkout_state
match '/checkout' => 'checkout#edit', :state => 'client_details', :as => :checkout
It looks like respond_with does different things depending upon the HTTP verb and whether the resource has errors. See here and here.
The following code worked for me:
def update
if #order.update_attributes(params[:order]) && #order.next
respond_with(#order) { |format| format.html { redirect_to checkout_state_url(#order.state) } }
else
respond_with(#order) { |format| format.html { render :edit } }
end
end
I have a set of API routes in rails as follows
namespace "api" do
namespace "v1" do
resources :users do
resources :posts
resources :likes
...
end
end
end
So far, so good. I can GET /api/v1/users/fred_flintstone and retrieve all of the information for that user.
What I would like to do now is add the concept of "me" (ala facebook) such that if the user is authenticated (fred_flintstone), I can also do the following
GET /api/v1/me
GET /api/v1/me/posts
...
I require both sets of routes. So I want to achieve the same results either using GET /api/v1/me/posts OR GET /api/v1/users/fred_flintstone/posts.
I've been through the route tutorial and have googled so a pointer would be as much appreciated as a direct answer.
EDIT:
What I've done that has worked is pretty hacky. I've created a second set of entries in the routes table using a scope:
scope "/api/v1/me", :defaults => {:format => 'json'}, :as => 'me' do
resources :posts, :controller => 'api/v1/users/posts'
resources :likes, :controller => 'api/v1/users/likes'
...
end
And then I added a set_user method that tests for the presence of params[:user_id]. I'm really looking for a way to DRY this up.
What about leaving the routes the way they are in your post, and just solving this inside the controller?
Heres a before_filter that you could apply to all of the routes you have which pull a User from a :user_id.
# Set the #user variable from the current url;
# Either by looking up params[:user_id] or
# by assigning current_user if params[:user_id] = 'me'
def user_from_user_id
if params[:user_id] == 'me' && current_user
#user = current_user
else
#user = User.find_by_user_id params[:user_id]
end
raise ActiveRecord::RecordNotFound unless #user
end
Then in your controller functions you can just use the #user variable without having to worry about whether the user passed a user_id, or me.
Hope that helps! :)
EDIT:
Lemme take another shot, given your comments.
How about a function that lists all the resources you wish to access via both the standard routes and the /me route. Then you can just use the function in both the namespaces you require.
routes.rb
# Resources for users, and for "/me/resource"
def user_resources
resources :posts
resources :likes
...
end
namespace 'api' do
namespace 'v1' do
resources :users do
user_resources
end
end
end
scope '/api/v1/:user_id', :constraints => { :user_id => 'me' },
:defaults => {:format => 'json'}, :as => 'me' do
user_resources
end
# We're still missing the plain "/me" route, for getting
# and updating, so hand code those in
match '/api/v1/:id' => 'users#show', :via => :get,
:constraints => { :id => 'me' }
match '/api/v1/:id' => 'users#update', :via => :put,
:constraints => { :id => 'me' }
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
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" })