How to manage the path to a template

146 Views Asked by At

In my view directory I have two subdirectory master/hotels and cruds. In cruds I have several files(new, index, edit, show). master only has index file. Now I set "index" in cruds as root and I can link directory to "new" by add this path in a button like this:

= link_to "new", url_for(new_master_hotels_path), class: "btn btn-white btn-sm"

The problem is, I cannot link to other path like edit_master_hotels_path ore show_show_hotels_path and I don't know why "new" can and others cannot. I use wice_grid plugin in my project. my routes here

# -*- coding: utf-8 -*-
SptHunter::Application.routes.draw do
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
devise_for :admin_users
resources :flights

mount RailsAdmin::Engine => '/rails_admin', :as => 'rails_admin'
resources :dashboard, only: [:index]
root to: 'master/hotels#index'
resources :reservations do
 collection do
  get 'callcenter'
 end
end

concern :importable do
 collection do
  post :import
 end
end

#resources :products do
#  collection { post :import }
#end
# マスタ
namespace :master do
  # マスタ(商品)
 resources :hotels, concerns: [:importable]
 resources :flights, concerns: [:importable]
 resources :options, concerns: [:importable]
 resources :events, concerns: [:importable]
 resources :products, concerns: [:importable]
 resources :hotel_room_stocks, concerns: [:importable]

 # マスタ(取引先)
 resources :agencies, concerns: [:importable]
 resources :companies, concerns: [:importable]
 resources :clients, concerns: [:importable]

 # マスタ(設定)
 resources :product_categories, concerns: [:importable]
 resources :delivery_categories, concerns: [:importable]
 resources :airports, concerns: [:importable]
 resources :areas, concerns: [:importable]
 resources :prefectures, concerns: [:importable]
 resources :hotel_rooms, concerns: [:importable]

 # マスタ(社内)
 resources :admin_users, concerns: [:importable]
 resources :admin_news, concerns: [:importable]

 end
end

and my view structure is: -master/hotels(index) -cruds(new,form,show,edit,index) hope it help.

2

There are 2 best solutions below

4
On BEST ANSWER

If you have subdirectory a & b, your routes should accommodate them like this:

#config/routes.rb
namespace 'subdirectory' do
   #routes here
   resources :demo
end

This basically means you have to maintain the resource-based Rails routing infrastructure within the subdomain, meaning your routes will basically be the same as if you didn't have a subdirectory set up.


Update

Surely you could fix your routes to be DRY like this:

namespace :master do
  # マスタ(商品)
 resources :hotels, :flights, :options, :events, :products, :hotel_room_stocks, concerns: [:importable]

 # マスタ(取引先)
 resources :agencies, :companies, :clients, concerns: [:importable]

 # マスタ(設定)
 resources :product_categories, :delivery_categories, :airports_categories, :areas, :prefectures, :hotel_rooms, concerns: [:importable]

 # マスタ(社内)
 resources :admin_users, :admin_news concerns: [:importable]

 end
0
On
class CrudsController < ApplicationController
  before_action :load_crud
  before_action :set_crud, only: [:show, :edit, :update, :destroy]

  def load_crud

  end

  # GET /cruds
  # GET /cruds.json
  def index
    @items_grid = initialize_grid(@model)

    @csv = CSV.generate() do |csv|
      csv << @model.column_names
      @model.all.each do |item|
        csv << item.attributes.values_at(*@model.column_names).map{|i| i.to_s.encode("cp932", "UTF-8")}
      end
    end

    @crud = @model.order(:name)
    respond_to do |format|
      format.html { render template: 'cruds/index'}
      format.csv { send_data @csv }
      #format.xls # {send_data @product.to_csv (col_sep: "|t")}
    end

    # render template: 'cruds/index'
  end

  # GET /cruds/1
  # GET /cruds/1.json
  def show
    render template: 'cruds/show'
  end

  # GET /cruds/new
  def new
    @crud = @model.new
    render template: 'cruds/new'
  end

  # GET /cruds/1/edit
  def edit
    render template: 'cruds/edit'
  end

  # POST /cruds
  # POST /cruds.json
  def create
    @crud = @model.new(params[:crud])

    respond_to do |format|
      if @crud.save
        format.html { redirect_to [:master, @crud], notice: 'Crud was successfully created. #{undo_link} ' }
        format.json { render action: 'show', status: :created, location: @crud }
      else
        format.html { render action: 'new' }
        format.json { render json: @crud.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cruds/1
  # PATCH/PUT /cruds/1.json
  def update
    respond_to do |format|
      if @crud.update(crud_params)
        format.html { redirect_to [:master, @crud], notice: 'Crud was successfully updated. #{undo_link}' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @crud.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cruds/1
  # DELETE /cruds/1.json
  def destroy
    @crud.destroy
    respond_to do |format|
      format.html { redirect_to action: :index , :notice => 'Successfully destroyed product. #{undo_link}'}
      format.json { head :no_content }
    end
  end



  def import
       @crud.import(params[:file])
      redirect_to root_url, notice: "Products imported."
      render :template => 'cruds/_form'
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_crud
      @crud = @model.find_by_id(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def crud_params
      params[@hash].permit(@model.attribute_names)
    end

    def undo_link
      view_context.link_to("undo", revert_version_path(@product.versions.scoped.last), :method => :post)
    end
end

this is my controller file. And the rake routes command show like this:

  import_master_hotels POST   /master/hotels/import(.:format)                master/hotels#import