2016-03-05 47 views
0

用戶的Rails這實在是一個小白的問題,但我已經搜索了很多,並沒有發現什麼對我的解決方案。2類和設計

A具有與2種用戶應用。客戶和承包商。我嘗試設計建立兩個,我可以簽署承包商的工作,並與客戶一樣都在db等好,但有些事情是行不通的。

首先,我不能在登錄窗體中添加承辦公司名稱(我已經做了很多,但知道不工作),我做遷移等,但不能顯示在表格符號。我沒有錯誤,只是沒有表演。

最後,我儘量讓客戶生成項目,並有他的身份證與

current_cutomer.projects.build(project_params) 

,我得到一個關於參數等

Sry基因的錯誤,我的英語

projects_controller。 rb

class ProjectsController < ApplicationController 

before_action :find_project, only: [:show, :quote, :edit, :update] 
before_action :authenticate_cus!, except: [:index, :show] 

def home 
end 

def index 
@projects = Project.all.order("created_at DESC") 
end 

def show 
end 

def quote 
end 

def new 
@project = current_customer.projects.create 
end 

def create 
@project = current_customer.projects.create(project_params) 


if @project.save 
    redirect_to @project 
else 
    render 'new' 
end 
end 

def edit 
end 

def update 
if @project.update(project_params) 
    redirect_to @project 
else 
    render 'edit' 
end 
end 

private 

def project_params 
params.require(:project).permit(:name, :description, :date, :budget, :category_id, :location, :image) 
end 

def find_project 
    @project = Project.find(params[:id]) 
end 
end 

schema.rb

ActiveRecord::Schema.define(version: 20160305071807) do 

create_table "active_admin_comments", force: :cascade do |t| 
t.string "namespace" 
t.text  "body" 
t.string "resource_id", null: false 
t.string "resource_type", null: false 
t.string "author_type" 
t.integer "author_id" 
t.datetime "created_at" 
t.datetime "updated_at" 
end 

add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id" 
add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace" 
add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id" 

create_table "admin_users", force: :cascade do |t| 
t.string "email",     default: "", null: false 
t.string "encrypted_password",  default: "", null: false 
t.string "reset_password_token" 
t.datetime "reset_password_sent_at" 
t.datetime "remember_created_at" 
t.integer "sign_in_count",   default: 0, null: false 
t.datetime "current_sign_in_at" 
t.datetime "last_sign_in_at" 
t.string "current_sign_in_ip" 
t.string "last_sign_in_ip" 
t.datetime "created_at",       null: false 
t.datetime "updated_at",       null: false 
end 

add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true 
add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true 

create_table "categories", force: :cascade do |t| 
t.string "name" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
end 

create_table "contractors", force: :cascade do |t| 
t.string "email",     default: "", null: false 
t.string "encrypted_password",  default: "", null: false 
t.string "reset_password_token" 
t.datetime "reset_password_sent_at" 
t.datetime "remember_created_at" 
t.integer "sign_in_count",   default: 0, null: false 
t.datetime "current_sign_in_at" 
t.datetime "last_sign_in_at" 
t.string "current_sign_in_ip" 
t.string "last_sign_in_ip" 
t.datetime "created_at",       null: false 
t.datetime "updated_at",       null: false 
t.string "company_name" 
end 

add_index "contractors", ["email"], name: "index_contractors_on_email", unique: true 
add_index "contractors", ["reset_password_token"], name: "index_contractors_on_reset_password_token", unique: true 

create_table "customers", force: :cascade do |t| 
t.string "email",     default: "", null: false 
t.string "encrypted_password",  default: "", null: false 
t.string "reset_password_token" 
t.datetime "reset_password_sent_at" 
t.datetime "remember_created_at" 
t.integer "sign_in_count",   default: 0, null: false 
t.datetime "current_sign_in_at" 
t.datetime "last_sign_in_at" 
t.string "current_sign_in_ip" 
t.string "last_sign_in_ip" 
t.datetime "created_at",       null: false 
t.datetime "updated_at",       null: false 
end 

add_index "customers", ["email"], name: "index_customers_on_email", unique: true 
add_index "customers", ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true 

create_table "projects", force: :cascade do |t| 
t.string "name" 
t.text  "description" 
t.integer "budget" 
t.date  "date" 
t.string "location" 
t.integer "category_id" 
t.datetime "created_at",   null: false 
t.datetime "updated_at",   null: false 
t.string "image_file_name" 
t.string "image_content_type" 
t.integer "image_file_size" 
t.datetime "image_updated_at" 
t.integer "customer_id" 
end 

add_index "projects", ["category_id"], name: "index_projects_on_category_id" 
add_index "projects", ["customer_id"], name: "index_projects_on_customer_id" 

end 

形式爲承包

<h2>Sign up</h2> 

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
<%= f.error_notification %> 
<h1>New Contractor</h1> 
<div class="form-inputs"> 
    <%= f.input :company_name, required: true, autofocus: true %> 
    <%= f.input :email, required: true %> 
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %> 
    <%= f.input :password_confirmation, required: true %> 
</div> 

<div class="form-actions"> 
    <%= f.button :submit, "Sign up" %> 
</div> 
<% end %> 

<%= render "contractors/shared/links" %> 
+0

這裏最好的答案是不使用兩個用戶類 - 設計並沒有內置支持這一點。使用角色。 – max

+0

那麼只有一個用戶並給他2個角色?對不起,我是新來的鐵路和編程。 – EQuimper

+0

除了@Max答案,你可能想看看[中色器件的wiki的解釋(https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-角色) – rdupz

回答

0

最後我做了兩個用戶模型色器件和應用控制器我寫

devise_group :user, contains: [:customer, :company] 

現在我可以用

@project = current_user.projects.build(project_params) 

一切都好,只需要給予權限。