Ruby on rails 创建表单上btn组的单表继承

Ruby on rails 创建表单上btn组的单表继承,ruby-on-rails,forms,ruby-on-rails-4,form-for,single-table-inheritance,Ruby On Rails,Forms,Ruby On Rails 4,Form For,Single Table Inheritance,我一直在想这个单表继承的例子。我理解它背后的概念,但通过阅读网站上的其他帖子,我不确定如何使它在我的示例中起作用。我使用STI是因为我有两种类型的项目——公共项目和私人项目。它们背后的逻辑是相同的,它们存储在数据库中的数据将是相同的,无论是公共的还是私有的。我只是打算在将来为用户实现一些授权和角色,这些授权和角色根据项目是公共的还是私有的略有不同。所以我的问题是,还需要采取哪些步骤才能使这项工作发挥作用……到目前为止,我已经: 1) 通过迁移将type_列添加到项目中…我的项目模型为 PROJE

我一直在想这个单表继承的例子。我理解它背后的概念,但通过阅读网站上的其他帖子,我不确定如何使它在我的示例中起作用。我使用STI是因为我有两种类型的项目——公共项目和私人项目。它们背后的逻辑是相同的,它们存储在数据库中的数据将是相同的,无论是公共的还是私有的。我只是打算在将来为用户实现一些授权和角色,这些授权和角色根据项目是公共的还是私有的略有不同。所以我的问题是,还需要采取哪些步骤才能使这项工作发挥作用……到目前为止,我已经:

1) 通过迁移将type_列添加到项目中…我的项目模型为

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

非常感谢您的帮助,

我不确定身份验证是否正确,但是您可以创建一个类型字段,告诉您项目是公共的还是私有的,例如is\u public,而不是为public和private使用其他类


另一个选择是使用一个非常简单和直接的方法。这是我的首选选项,但您需要使用最新版本的rails。

Hi Ryan,“is_public”类型字段不会在数据库中留下大量空值吗?有人警告我要避免这样做&使用单表继承。。。我不熟悉枚举…我使用的是rails 4.0.4。但是它看起来很有趣…使用这样的东西比STI有什么好处(&有什么缺点吗…从我所有的研究来看,STI似乎是继续进行下去的方式,但我意识到枚举是非常新的)。感谢您的回复我想如果您计划在私人/公共项目中添加特定功能,那么STI是一条可行的道路,您可以扩展该功能,否则我会追求简单性。是的,唯一的区别在于未来的角色和权限(所有用户都可以是公共项目的合作者……只有受邀请的用户才能是私人项目的合作者等).你不太确定如何通过STI完成,是吗?经过深思熟虑…我认为你可能是对的。如果公共和私人项目之间的唯一区别是用户访问权限和各种角色授权…最好将其视为一个标志。我只是按照你的建议使用了一个is_private?类型字段…即使是你gh这与关于STI的原始问题并不完全一致,我将标记你的答案为正确,因为这是一种更好的方式。thx,
class PublicProject < Project
end
class PrivateProject < Project
end
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
<% 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>
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