2 种用户 Rails 使用 Devise
2 kind of user Rails with Devise
这确实是一个菜鸟问题,但我搜索了很多,但没有找到适合我的解决方案。
一个应用程序有两种用户。客户和承包商。我尝试设计来构建两者,并且我可以与承包商签约并且与客户相同的工作在数据库等中都很好。但是有些事情不起作用。
首先我无法在承包商的登录表单中添加公司名称(我已经做了很多但知道行不通)我进行了迁移等但无法在登录中显示形式。我没有错误只是没有显示。
最后我尝试让客户构建项目并让他的 Id 与
current_cutomer.projects.build(project_params)
我收到关于参数等的错误
对不起我的英语
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" %>
最后我用设计做了两个用户模型,在我写的应用程序控制器中
devise_group :user, contains: [:customer, :company]
现在我可以使用
@project = current_user.projects.build(project_params)
一切正常,只需要授予权限即可。
这确实是一个菜鸟问题,但我搜索了很多,但没有找到适合我的解决方案。
一个应用程序有两种用户。客户和承包商。我尝试设计来构建两者,并且我可以与承包商签约并且与客户相同的工作在数据库等中都很好。但是有些事情不起作用。
首先我无法在承包商的登录表单中添加公司名称(我已经做了很多但知道行不通)我进行了迁移等但无法在登录中显示形式。我没有错误只是没有显示。
最后我尝试让客户构建项目并让他的 Id 与
current_cutomer.projects.build(project_params)
我收到关于参数等的错误
对不起我的英语
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" %>
最后我用设计做了两个用户模型,在我写的应用程序控制器中
devise_group :user, contains: [:customer, :company]
现在我可以使用
@project = current_user.projects.build(project_params)
一切正常,只需要授予权限即可。