setting up named routes for resouce routes in rails - ruby-on-rails

I have what I think should be a simple task - but it's causing me a real headache.
I've added a resource route to my app using -
resources :cars
I want the user to be able to edit a car by going to
mydomain.com/CARNAME
In the edit action of the controller -
#donor = Donor.find_by_name(params[:car])
This page shows by setting up a named route -
match "/:car" =>"cars#edit", :as => :car
However when I try and submit the form on that page I get an error?
How can I set up the show action to have a named route to correspond with the edit route?
My desired show url would be something like
mydomain.com/CARNAME/savecomplete

In routes.rb
match "/:car" =>"cars#edit", :as => :edit_car
match "/:car/savecomplete" =>"cars#show", :as => :savecomplete
In cars_controller.rb update action
format.html { redirect_to(savecomplete_path(#car) }

Related

How to set route for a rendered action? Rails

i'm new to mvc, rails and web development and i'm facing a problem:
I have an action(show) and a view for this action.
The view for show submits a form_tag to another action, that renders the action show.
The problem is, I have no idea how to set a route for the action that renders show.
Right now my routes.rb is:
resources :meals do
collection do
get "meals/:id", to: "meals#show"
end
end
Tried to add these but didn't work:
match "meals/:id/calculate" , :to => "meals#calculate",:via => [:get]
and:
get "meals/:id/calculate", to => "meals#calculate"
resources :meals generate path to show action.
Run rake routes or open in a browser 'http://localhost:3000/rails/info/routes` to see list of generated routes.
To add calculate use member:
resources :meals do
get :calculate, on: :member
end

Rails redirection to show when redirected to other action in same class

My Task is to submit a form to place_order action inside Checkout controller.
This is how I wrote form in my view file i.e
<%= form_for (#order), url: {action: "place_order"} do |f| %>
It does reach inside this method and as I save object i want to redirect to some other method in the same class. This method name is thank_you. My code looks like this inside place_order method
if #order.save
redirect_to :action => 'thank_you'
else
...
end
But it redirects to show method of this class. If I change redirect to other class, it redirects fine but on other action of same controller, it always redirects to show.
Here is how I defined my routes
resources :checkout
resources :photos
devise_for :users
resources :carts
post 'checkout/place_order'
match 'checkout/thank_you', to: 'checkout#thank_you', via: [:get]
I need some expert opinion on this. Please help.
Move your thank_you route above resources :checkout.
From Rails guides:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action's
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.

Rails: custom url

I am newbie in rails. In my demo app I can show all my products list in http://localhost:3000/products and each product has show, edit, delete option. When I click on the show link, I go to the specific product and I get a url for that product like
http://localhost:3000/products/5 (that is id after /products/) but I want two things to add
1) When I click on show button my url should be something like this
http://localhost:3000/products/display
Id should not be after products. Only display should be.
2)and I want to add another button from where I can show all the products list in another url like http://localhost:3000/products/displayall that is my all products will show both in http://localhost:3000/products and http://localhost:3000/products/displayall
You could use this to overwrite the default CRuD behavior
resource :products do
get ':id/show', :to => :show
get ':id/display', :to => :show
get 'displayall', :to => :index, :on => :collection
end
http://guides.rubyonrails.org/routing.html
As in the link below described under 2.10.1 use :on => :member to add a restful link who is able to recognize /products/:id/display otherwise the example below.
You've cited the show, edit, and delete actions, so you're presumably defining resourceful routes. If this is correct, you will be able to add custom routes to your Product routes via a collection block:
# config/routes.rb
resources :products do
collection do
get 'display'
get 'displayall'
end
end
This adds the following two set of named routes:
display_products GET /products/display(.:format) products#display
displayall_products GET /products/displayall(.:format) products#displayall
These routes point to the products#display and products#displayall controller actions, respectively.

Conflict routes and params name in Ruby on Rails

In my website, users can visit category via example.com/category1, example.com/category2, etc. So I write the route rule as below:
match '/:category' => 'home#category', :constraints => ShowCategory.new
ShowCategory is a Class that make sure the category user visited is exist. At the same time, users can specify their personal domain name, then his/her profile page can be visited via url like example.com/peter. So There is another route rule:
match '/:user_domain' => 'Profiles#show'
Because I use the :constraints for category route, the routes didn't conflict. However, in the Profiles#show action I always get the parameters {'category' => 'peter'}, not {'user_domain' => 'peter'}.
How can I correct the parameter name? I don't want a parameter named category in the profiles controller.
Thanks.
Well, the way you're naming your routes doesn't make much sense; it's making the routing engine confused. It should be categories/1 rather than category1.
As for the profiles, I guess the first rule and the to_param method should be enough.
# routes.rb
profile '/:user_domain', controller: 'profiles', action: 'show'
resources :profiles
resources :categories
# profile.rb
def to_param
self.user_domain
end

How to swap a route's action and param id around

I have a url redirect controller called "go". It can redirect topic model and company models. The route.rb looks like this:
resources :go, only: [] do
member do
get :topic
get :company
end
end
Currently it produce the following route:
topic_go GET /go/:id/topic(.:format) go#topic
company_go GET /go/:id/company(.:format) go#company
Is there a good way to make the route to be like this:
/go/topic/:id
/go/company/:id
Just remove the member block entirely and add these two lines above resources :go.
get '/go/topic/:id' => 'go#topic', :as => :topic_go
get '/go/company/:id' => 'go#company', :as => :company_go

Resources