创建表单上使用btn-group的单表继承

时间:2014-04-22 06:41:06

标签: ruby-on-rails forms ruby-on-rails-4 form-for single-table-inheritance

我一直试图围绕这个单表继承示例。我理解它背后的概念,但我不确定阅读网络上的其他帖子和在这个网站上如何使它适用于我的例子。我正在使用STI,因为我有两种类型的项目 - 公共和私人。它们背后的逻辑是相同的,无论是公共还是私有,它们存储在数据库中的数据都是相同的。我只打算实施一些授权和未来用户的角色根据项目是公共还是私有而略有不同。所以我的问题是,到目前为止我还需要采取哪些步骤才能完成这项工作......

1)通过迁移向项目添加了一个type_column ...我的项目模型是

PROJECT.RB

class Project < ActiveRecord::Base
  has_many :users
  has_many :versions, dependent: :destroy
  validates :title, presence: true, length: { maximum: 100 }
  validates :background, presence: true
  validates :user_id, presence: true

  default_scope -> { order('created_at DESC') }
end

2)我为PublicProjects&amp;创建了单独的类。 PrivateProjects ......那些模型是:

PUBLIC_PROJECT.RB

class PublicProject < Project
end

PRIVATE_PROJECT.RB

class PrivateProject < Project
end

所以现在我想知道我的控制器需要重新考虑什么(我想保留单个Projects控制器)......然后还需要在我的'new'表单视图中进行更改。一步一步的帮助将使这项工作的代码非常感激。控制器/视图文件是:

PROJECTS_CONTROLLER.RB

class ProjectsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :new, :edit, :update]

  def new
    @project = Project.new
  end

  def show
     @project = Project.find(params[:id])
     @user = User.where(:id => @project.user_id).first
  end

  def index
    @projects = Project.paginate(page: params[:page])
  end

  def create
    @project = current_user.projects.build(project_params)
    if @project.save
      flash[:success] = "Welcome to your new project."
      redirect_to @project
    else
      render 'new'
    end
  end

  def edit

  end

  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
      flash[:success] = "Project Created"
      redirect_to @project
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "Project destroyed"
    redirect_to users_path
  end

  private

    def project_params
      params.require(:project).permit(:title, :background)
    end

end

NEW.HTML.ERB(新项目视图/表格)

<% provide(:title, 'New Project') %>
<h1>Create a new project</h1>

<div class="row-fluid">
  <div class="col-md-5 no-pad offset3">

    <%= bootstrap_form_for @project do |f| %>

      <%= render 'shared/error_messages', object: f.object %>

      <%= f.text_field :title %>

      <%= f.text_area :background %>

      <div class="row-fluid">
        <div class="no-pad col-md-6">
          <h5>Is this project public or private?</h5>
          <div class="btn-group">
            <button type="button" class="btn btn-default"><%= image_tag "globe.png" %> Public</button>
            <button type="button" class="btn btn-default"><%= image_tag "lock.png" %> Private</button>
          </div>
          <script>
          $(".btn-group > .btn.btn-default").click(function(){
              $(".btn-group > .btn.btn-default").removeClass("active");
              $(this).addClass("active");
          });
          </script>
        </div>
        <div class="col-md-6">
          Some static graphics
        </div>
      </div>

      <br clear="all"><br/>
      <%= f.submit "Create your project", class: "btn btn-lg btn-primary" %>
    <% end %>
  </div>

</div>

最后,我已经看到了一些进入路径文件的STI示例(虽然我已经看到这种情况不经常被推荐)。但为了以防万一:

的routes.rb

ProductionApp::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  resources :projects do
    resources :versions
  end

  # get "static_pages/home"
  # get "static_pages/help"
  # get "static_pages/about"
  #The original routes above map to...
  root  'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signout', to: 'sessions#destroy',     via: 'delete'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
end

非常感谢您的帮助,

1 个答案:

答案 0 :(得分:1)

我不确定身份验证,但是您可以创建一个类型字段来告诉您该项目是公共还是私有,例如is_public。而不是使用公共和私有的其他类。

另一种选择是使用enum release in rails 4 - 非常简单直接。这是我的首选,但您需要使用最新版本的rails。