rails-routing

rails remove controller path from the url

自作多情 提交于 2019-12-01 08:45:24
I have the following loop in my view <% @posts.each do |post| %> <%= link_to post do %> Some html <% end %> <% end %> The above code will generate link as localhost:3000/posts/sdfsdf-sdfsdf But I would like to have the link as localhost:3000/sdfsdf-sdfsdf Here is my route resources :posts, except: [:show] scope '/' do match ':id', to: 'posts#show', via: :get end You could do this: #config/routes.rb resources :posts, path: "" #-> domain.com/this-path-goes-to-posts-show -- Also, make sure you put this at the bottom of your routes; as it will override any preceding routes. For example, domain.com

/YYYY/MM/Title-Slug URL structure with Friendly_Id Solution Chokes on #edit

落花浮王杯 提交于 2019-12-01 06:53:51
问题 Based on guidance I got in my earlier question in resolving my original issue to implement /YYYY/MM/Slug URL structure, I'm hoping to get some help in resolving an error I'm receiving when attempting to edit a post: No route matches [PATCH] "/blog/2015/09/example-post/blog/2015/09/example-post" Here are all the files in question (working off the same very simple scaffolded blog): $ rails new blog [...] $ cd blog # (Add friendly_id to Gemfile & install) $ rails generate friendly_id $ rails

How to set default format for routing in Rails?

左心房为你撑大大i 提交于 2019-12-01 05:38:23
There is the following code for routing: resources :orders, only: [:create], defaults: { format: 'json' } resources :users, only: [:create, :update], defaults: { format: 'json' } resources :delivery_types, only: [:index], defaults: { format: 'json' } resources :time_corrections, only: [:index], defaults: { format: 'json' } It is possible to set default format for all resources using 1 string without 'defaults' hash on each line? Thanks. Try something like this: scope format: true, defaults: { format: 'json' } do resources :orders, only: [:create] resources :users, only: [:create, :update]

How to set default format for routing in Rails?

爷,独闯天下 提交于 2019-12-01 03:22:59
问题 There is the following code for routing: resources :orders, only: [:create], defaults: { format: 'json' } resources :users, only: [:create, :update], defaults: { format: 'json' } resources :delivery_types, only: [:index], defaults: { format: 'json' } resources :time_corrections, only: [:index], defaults: { format: 'json' } It is possible to set default format for all resources using 1 string without 'defaults' hash on each line? Thanks. 回答1: Try something like this: scope format: true,

Routing in Rails. Dots in URL [duplicate]

拜拜、爱过 提交于 2019-12-01 00:25:52
问题 This question already has answers here : Why do routes with a dot in a parameter fail to match? (3 answers) Closed 4 years ago . I have overwritten to_param method in my Category model def to_param name end And routes.rb get '/:id' => 'categories#show', :as => :category When name parameter doesn't contain any dots ( foobar ), all works right, but when it does ( f.o.o.b.a.r ) I get an error No route matches [GET] . So my question is: is it possible to use dots in routing like a part of name of

Why rails app is redirecting unexpectedly instead of matching the route?

佐手、 提交于 2019-11-30 23:47:13
I asked this question earlier and thought it was fixed, but it's not. Previous question here My problem is I am trying to set my routes so that when I type in localhost:3000/sites/admin It should redirect to localhost:3000/en/sites/admin here is my routes.rb file scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do get "log_out" => "sessions#destroy", as: "log_out" get "log_in" => "sessions#new", as: "log_in" resources :sites, except: [:new, :edit, :index, :show, :update, :destroy, :create] do collection do get :home get :about_us get :faq get :discounts get :services get :contact

Why rails app is redirecting unexpectedly instead of matching the route?

我们两清 提交于 2019-11-30 18:53:09
问题 I asked this question earlier and thought it was fixed, but it's not. Previous question here My problem is I am trying to set my routes so that when I type in localhost:3000/sites/admin It should redirect to localhost:3000/en/sites/admin here is my routes.rb file scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do get "log_out" => "sessions#destroy", as: "log_out" get "log_in" => "sessions#new", as: "log_in" resources :sites, except: [:new, :edit, :index, :show, :update,

grouping controller in subdirectories for nested resources

牧云@^-^@ 提交于 2019-11-30 13:01:24
问题 I would like to organize my controllers in subdirectories. Here is an example: routes.rb: resources :locations do resources :users end I would like to put my controller in the appropriate subdirectory: app/controllers/locations/users_controller.rb and the url would be (standard): /locations/1/users /locations/1/users/new /locations/1/users/10/edit ... If i had a namespace in my routes I could change my users_controller.rb to class Locations::UsersController < LocationsController end but it

Adding prefix to a named route helper under namespace

好久不见. 提交于 2019-11-30 10:20:09
This is how a common namespace looks like. namespace :admin do resources :posts end And it creates a named route like this one; new_admin_post_path Here's my question; how can I add a prefix (like "new" in this example) to a named route under namespace? Let's say my route definition likes this one; namespace :admin do get 'post/new' => 'posts#new', as: 'post' end And it's creates a named route like; admin_post_path I want to add "new" prefix to this named route and make it look like new_admin_post_path and I don't want to use resources . Just try the code in routes. namespace :admin, as: '' do

Rails 3 link or button that executes action in controller

最后都变了- 提交于 2019-11-30 09:14:18
In RoR 3, I just want to have a link/button that activates some action/method in the controller. Specifically, if I click on a 'update_specs' link on a page, it should go to 'update_specs' method in my products controller. I've found suggestions to do this on this site: link_to "Update Specs", :controller => :products, :action => :update_specs However, I get the following routing error when I click on this link: Routing Error No route matches {:action=>"update_specs", :controller=>"products"} I've read up on routing but I don't understand why I should have to route this method if all other