How to set route for a rendered action? Rails - ruby-on-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

Related

creating custom routes in rails

I am creating a custom route like:
namespace :admin do
root 'users#index'
resources :users do
get 'admin_login' => 'users#admin_login'
end
end
But when I see with rake routes:
admin_user_admin_login GET /admin/users/:user_id/admin_login(.:format) admin/users#admin_login
Why :user_id is added here?
I just want it without :user_id.
Because you are creating a custom route within the users resource. Rails is doing exactly what you are telling it to do. You would like to show the "admin_login" route for a specified user (that's what you're currently telling rails to do).
Move the:
get 'admin_login' => 'users#admin_login'
Line of code outside of the resources block and you'll be able to create your custom route.
You need to specify an on option to tell Rails that it works on a collection and not a member resource. According to the official Rails routing guide
You can leave out the :on option, this will create the same member
route except that the resource id value will be available in
params[:photo_id] instead of params[:id].
You can also remove the => 'users#admin_login' part as that is the default behavior.
So the solution to your problem is to add on: :collection or place it inside a block like
namespace :admin do
root 'users#index'
resources :users do
collection do
get 'admin_login'
end
end
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.

Rails custom route not working

Feel like I'm doing this right, but apparently not.
I have a restful resource, Posts, with index, show, new, update, edit, etc actions in the controller. In routes, I have
resources :posts
I wanted to make the index action occur at the URL '/archive' instead of '/posts'
So I added this line in the routes.rb file, after the resources one:
match '/archive', to: "posts#index"
But when I click on a link to posts_path, it still goes to /post (though if I type in /archive as a url, it works -- not ideal, though). Confused. Could this have to do with my having installed friendly_id?
resources :posts, except: [:index]
get 'archive' => 'posts#index', as: :posts
You need to use something like match '/archive', :to => 'posts#index', :as => 'archived'. Then you will have a new route to the tune of archived_posts_path. The method posts_path does not dynamically changed based on custom matchers. You can always run rake routes to see a list of routes for your site.

Rails routes to show action on custom route

I am trying to add a customer action to one of my resources, therefore I created a custom route:
namespace :admin do
resources :subscriptions
match 'subscriptions/summary', :to => 'subscriptions#summary', :as => 'subscriptions_summary'
end
In my rake routes I'm getting the following output:
admin_subscriptions_summary /admin/subscriptions/summary(.:format) spree/admin/subscriptions#summary
The problem now is, whenever I try to create a link to the summary action, I get following error:
Missing template spree/admin/subscriptions/show
Why is my app confusing the show action with the summary action?
namespace :admin do
resources :subscriptions do
collection do
get 'summary'
end
end
end
solved it.

Resources