如何管理模板的路径

时间:2014-04-02 09:59:19

标签: ruby-on-rails wice-grid

在我的视图目录中,我有两个子目录master / hotels和cruds。在cruds我有几个文件(新的,索引,编辑,显示)。 master只有索引文件。现在我以root身份在cruds中设置“index”,我可以通过在这样的按钮中添加此路径将目录链接到“new”:

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

问题是,我无法链接到其他路径,如edit_master_hotels_path ore show_show_hotels_path,我不知道为什么“新”可以和其他人不能。我在我的项目中使用wice_grid插件。 我在这里的路线

# -*- 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

我的视图结构是: -master /酒店(指数) -cruds(新,形式,显示,编辑,索引) 希望它有所帮助。

2 个答案:

答案 0 :(得分:1)

如果你有subdirectory a& b,您的路线应为accommodate them like this

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

这基本上意味着您必须在子域内维护基于资源的Rails路由基础结构,这意味着您的路由基本上与您没有设置子目录的路由相同。


<强>更新

当然,您可以将您的路线修改为DRY:

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

答案 1 :(得分:0)

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

这是我的控制器文件。 rake routes命令显示如下:

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