2 типа пользователей Rails с Devise

Это действительно нубский вопрос, но я много искал и ничего не нашел для своего решения.

Есть приложение с двумя типами пользователей. Заказчик и Подрядчик. Я пытаюсь с разработкой построить и то, и другое, и эту работу я могу подписать с подрядчиком, и то же самое с заказчиком, все хорошо в БД и т. Д. Но некоторые вещи не работают.

Во-первых, я не могу добавить название компании в форму входа для подрядчика (я уже много раз это делал, но знаю, что это не работает). Я выполняю миграцию и т. д., но не могу показать в форме входа. У меня нет ошибки, просто не показывает.

Наконец, я пытаюсь заставить клиента построить проект и получить его идентификатор с

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

схема.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" %>

person EQuimper    schedule 05.03.2016    source источник
comment
Лучший ответ здесь - не использовать два пользовательских класса - Devise не поддерживает это. Используйте роли.   -  person max    schedule 05.03.2016
comment
Так что всего одного пользователя и дать ему 2 роли? Извините, я новичок в рельсах и программировании.   -  person EQuimper    schedule 05.03.2016
comment
В дополнение к ответу @max вы можете взглянуть на объяснение в вики устройства   -  person rdupz    schedule 05.03.2016
comment
@Emanuel Quimper – Что за сообщение об ошибке?   -  person Leventix    schedule 05.03.2016
comment
Поэтому я решил сделать 2 роли. И я добавляю в user_id (заказчик) и company_name (подрядчик). Думаешь, я в хорошем смысле?   -  person EQuimper    schedule 06.03.2016


Ответы (1)


Наконец, я сделал две пользовательские модели с devise и в контроллере приложения написал

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

Теперь я могу использовать

@project = current_user.projects.build(project_params)

Все в порядке, просто нужно дать разрешения.

person EQuimper    schedule 06.03.2016