I have a rails application that needs to search two different collections independently, but both are indexed in a single solr instance. However, when i try to search in one collection, when i get to the item i want displayed, it reroutes to the same id within the other collection. How should i adjust my routes.rb to remedy this?
Here is the code:
Rails.application.routes.draw do
get 'biofile/search'
get 'masterfile/search'
mount Blacklight::Engine => '/'
Blacklight::Marc.add_routes(self)
root to: "catalog#index"
concern :searchable, Blacklight::Routes::Searchable.new
resource :catalog, only: [:index], as: 'catalog', path: '/', controller: 'catalog' do
concerns :searchable
end
resource :masterfile, only: [:search], as: 'masterfile', path: '/masterfile', controller: 'masterfile' do
concerns :searchable
end
resource :biofile, only: [:search], as: 'biofile', path: '/biofile', controller: 'biofile' do
concerns :searchable
end
devise_for :users
concern :exportable, Blacklight::Routes::Exportable.new
resources :solr_documents, only: [:show], path: '/catalog', controller: 'catalog' do
concerns :exportable
end
resources :solr_documents, only: [:show], path: 'masterfile', controller: 'masterfile' do
concerns :exportable
end
resources :solr_documents, only: [:show], path: 'biofile', controller: 'biofile' do
concerns :exportable
end
resources :bookmarks do
concerns :exportable
collection do
delete 'clear'
end
end
get '/catalog/masterfile/', to: 'masterfile#search', as: 'masterfile'
get '/catalog/biofile/', to: 'biofile#search', as: 'biofile'
get '*path' => redirect('/')
end
are you asking about nested routes? you want the path to look like model1_model2_path? the below example will give you group_memberships_path to use in your view and the url would have the id of the group and the id of the membership like .../groups/22/membership/13/edit
use rails routes in rails 5.x or rake routes in rails 4 to see the list of your new routes and you can see the urls will take 2 id's that can be passed to your application.
# nested routes for groups
resources :groups do
resources :memberships
end
I think you'll need to override this method https://github.com/projectblacklight/blacklight/blob/9f676994f437f7664cfc2b5c4dffe4382a6d14d3/lib/blacklight/search_state.rb#L48 either by configuration or monkeypatch to behave differently based on some info in solr document
Related
I have two types of products that are nested under the same categories. The routes I have setup are
resources :categories, path: '/', only: [:show] do
resources :subcategories, path: '/', only: [:show] do
resources :amazon_products, path: '/', only: [:show]
resources :other_products, path: '/', only: [:show]
end
end
which I was previously accessing using this link
<%= link_to "View Product Page", [product.collection, product.category, product.subcategory, product], class: 'product__link' %>
which that resulted in something like this url after friendly_id finished with it
/cleansers/face-wash-and-cleansers/blemish-remedy-acne-treatment-gelee-cleanser
The issue is that the link only resolves for amazon_products and I'm not sure how to make it differentiate between the two. I think the problem is in the way I am referencing the path since when I enter rails routes in the console, I can see the two different paths there like so
category_subcategory_amazon_product
GET :category_id/:subcategory_id/:id(.:format)
amazon_products#show
category_subcategory_other_product
GET /:collection_id/:category_id/:subcategory_id/:id(.:format)
other_products#show
I tried specifically referencing the other product path using the link
category_subcategory_other_product_path(product.category, product.subcategory, product)
but it is giving me an ActiveRecord::RecordNotFound since its still looking in the wrong controller
app/controllers/amazon_products_controller.rb:5:in `show'
How do I tell rails to differentiate between these two resources?
When you boil it down, Rails routes are relatively simple patterns that match URIs and dispatch requests to the proper controller/method.
If you had specified your nested routes in their basic form:
resources :categories, only: [:show] do
resources :subcategories, only: [:show] do
resources :amazon_products, only: [:show]
resources :other_products, only: [:show]
end
end
The resulting URI patterns for the product level would look like:
/categories/<id>/subcategories/<id>/amazon_products/<id>
/categories/<id>/subcategories/<id>/other_products/<id>
Admittedly verbose, but obviously distinct, patterns.
The issue is that you are using path: '/' on the resources to remove everything unique about the resource routes. So, your product-level route patterns are:
category_subcategory_amazon_product: /<id>/<id>/<id>
category_subcategory_other_product: /<id>/<id>/<id>
Since the two patterns are identical, Rails falls back to the time honoured practice of matching the first one defined. You can demonstrate that for yourself by swapping :other_products so it is first; its controller will then be the one always invoked.
NB: It doesn't matter that you are using friendly-id -- that just changes what the IDs look like to the user, not the basic route pattern.
The solution is to simply reintroduce some uniqueness, at least at the product level.
resources :categories, path: '/', only: [:show] do
resources :subcategories, path: '/', only: [:show] do
resources :amazon_products, path: 'amazon', only: [:show]
resources :other_products, path: 'other', only: [:show]
end
end
The result will be route patterns that Rails can actually distinguish:
category_subcategory_amazon_product: /<id>/<id>/amazon/<id>
category_subcategory_other_product: /<id>/<id>/other/<id>
im currently trying to use the resource but one problem im having , that when i do the following
resource :orders
the route /orders dosent route to OrdersController#index rather it points to the show action of the controller, how can i fix this issue ?
becuase of this problem im having to do this which i feel is kinda hack and not good
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
this is my routes.rb file
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
get '/orders', to: 'orders#index'
get '/orders/:id', to: 'orders#show'
resources :users do
resource :orders, only: %i[show create index]
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
resource :sessions, only: [] do
post 'login', action: :create
post 'logout', action: :destroy
get 'login', action: :new
end
resources :products
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
thanks for your answer :)
Don't use
resource :orders
use
resources :orders
You would only use resource when the item orders is a single entity in your application... which is to say you're using the plural to refer to that one item.
Move the resource for orders outside of the Users routes.
Just and FYI, you can have both the full resource outside of Users and then those restricted routes inside Users, but I'm not sure what the goal is here so it is up to you to decide that.
Rails.application.routes.draw do
get '/carts', to: 'carts#index'
get '/payments', to: 'payments#index'
post '/payments', to: 'payments#add_credits'
resources :orders
resources :users do
resource :orders, only: %i[show create index] <-- not sure if this remains here
resource :carts, only: %i[create destroy], path: 'cart', as: 'cart'
end
...
I have the following routes:
authenticated :user do
scope module: 'admin', path: ':publisher_id' do
get 'settings/general', to: 'publishers#index', as: 'publisher_settings'
post 'settings/general', to: 'publishers#create'
put 'settings/general', to: 'publishers#update'
end
end
However the first as: can only be used once. How can I make this apply to all these routes? I want all of them to work for the publisher_settings_path
You can also pass the as option to namespaces and scopes in Rails Routes.
# prefix the routing helper name: +sekret_posts_path+ instead of +posts_path+
scope as: "sekret" do
resources :posts
end
Source: https://api.rubyonrails.org/v5.1/classes/ActionDispatch/Routing/Mapper/Scoping.html
This should also work though i think
Rails.application.routes.draw do
resources :publishers, only: [:create, :update, :index], path: 'settings/general', as: 'publisher_settings'
end
Get's me
publisher_settings GET /settings/general(.:format) publishers#index
POST /settings/general(.:format) publishers#create
publisher_setting PATCH /settings/general/:id(.:format) publishers#update
PUT /settings/general/:id(.:format) publishers#update
publisher_settings_path already returns the path of of the three routes, e.g. /admin/settings/general. So you don't need to set as: multiple times.
And actually setting the same as: multiple times for multiple routes would not make sense, it has to return one value.
I have an app with a parent resource (has_many) of patterns and a child resource (belongs_to) of snippets. My desire is to build a couple custom routes to specific pages and I am wondering the best way to do that. For now, here is what I have that is working, but I am wondering if there is a better way since the article I read on custom routing tells me this is not good practice.
I have purposefully not nested the resources because the snippets need to stand alone as well as be viewed within the context of their parent pattern.
The goal is to be able to create a few custom views like the following:
http://patterns.dev/patterns/:id/snippets //Got this one working, but incorrectly I believe
http://patterns.dev/patterns/:id/snippets/sort // Show all snippets for pattern to sort
http://patterns.dev/patterns/:id/images // Show all the images for patterns to moderate
routes.rb
Rails.application.routes.draw do
devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
resources :patterns
get 'patterns/:id/snippets' => 'patterns#snippets', as: 'pattern_snippets'
resources :snippets
root 'welcome#index'
end
I guess nested resources is what you need. You can specify just index action for nesting and keep all the snippets resources separately. Also you are to add some logic to snippets#index action to check params[:pattern_id]'s existance:
resources :patterns do
# this line will generate the `patterns/:id/snippets` route
# referencing to snippents#index action but within specific pattern.
# Also you have to add pattern_id column to snippets table to define the relation
resources :snippets, only: :index
end
resources :snippets
Use Collection Routes to make Rails to recognize paths like /patterns/:id/snippets/sort
resources :patterns do
resources :snippets, only: :index do
# this line will generate the `patterns/:id/snippets/sort` route
# referencing to snippets#sort action but again within specific pattern.
get :sort, on: :collection
end
end
resources :snippets
If you have Image model you can nest resources the same way like with snippets:
resources :patterns do
resources :images, only: :index
end
If it's just an action in patterns controller you can do:
resources :patterns do
get :images, on: :member
end
I have read up on the Rails Guides.
What I want to set up are the following routes that are routed to the 'profiles' controller:
GET profiles/charities - Should display all the charities
GET profiles/charties/:id should display a specfic charity
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor
I have created the profile controller and two methods: charities and donors.
Is this all I need?
The following will set up routes for what you want, but will map them to :index and :show of CharitiesController and DonorsController:
namespace :profiles do
# Actions: charities#index and charities#show
resources :charities, :only => [:index, :show]
# Actions: donors#index and donors#show
resources :donors, :only => [:index, :show]
end
When it's more appropriate to set up custom routes, something like this would do:
get 'profiles/charities', :to => 'profiles#charities_index'
get 'profiles/charities/:id', :to => 'profiles#charities_show'
get 'profiles/donors', :to => 'profiles#donor_index'
get 'profiles/donors/:id', :to => 'profiles#donor_show'
Here are relevant sections in the guide that you were going through:
Resource Routing: the Rails Default - Controller Namespaces and Routing
Non-Resourceful Routes - Naming Routes
The charities and donors seem to be nested resources. If so, in your config/routes.rb file you should have something like,
resources :profiles do
resources :charities
resources :donors
end
Because these are nested resources, you do not need the two methods named charities and donors in your profiles controller. In fact, depending on your app, you may need separate controllers and/or models for your charities and donors.