让我们假设您有两种特定类型的用户,即雇员和prestataires。首先,您使用一般信息(姓名,电子邮件等)填写用户表单。然后,您可以在prestataire或employeur表单之间进行选择,这将保存用户表单并将您重定向到题字的第二部分。雇员和prestataires模型都与用户具有belongs_to关系。
使用下面的代码,我已经能够创建并保存用户表单,被重定向到employeesur或prestataire表单。然而,当我创建一个employeeur时,我得到一个错误:#的未定义方法`users',尽管我的嵌套路由:
resources :users do
resources :employeurs
resources :prestataires
end
这是我的用户表单和控制器:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :civility, 'Titre de civilité: ' %><br>
<%= f.text_field :civility %>
</div>
.
.
.
<div class="actions">
<%= f.submit "Employeur" %>
<%= f.submit "Prestataire" %>
</div>
<% end %>
def create
@user = User.new(user_params)
用户控制器的这一部分创建一个实例并重定向到employeur或prestataire表单:
respond_to do |format|
if @user.save
if params[:commit] == 'Prestataire'
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }
format.json { render action: 'show', status: :created, location: @user }
end
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
雇员控制员:
class EmployeursController < ApplicationController
def index
@employeurs = Employeur.all
end
# GET /projets/1
# GET /projets/1.json
def show
end
def new
@user = User.find(params[:user_id])
@employeur = @user.build_employeur
end
# GET /projets/1/edit
def edit
end
def create
@user = User.find(params[:user_id])
@employeur = @user.create_employeur(employeur_params)
end
def create_employeur
@user = User.find(params[:user_id])
@employeur = Employeur.new(employeur_params)
respond_to do |format|
if @employeur.save
format.html { redirect_to user_employeur_path(@user, @employeur), notice: 'Employeur was successfully created.' }
format.json { render action: 'show', status: :created, location: @employeur }
else
format.html { render action: 'new' }
format.json { render json: @employeur.errors, status: :unprocessable_entity }
end
end
end
private
def employeur_params
params.require(:employeur).permit(:siren, :societe, :code_postal)
end
end
并且就业形式:
<%= form_for [@user, @employeur] do |f| %>
<% if @employeur.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employeur.errors.count, "error") %> prohibited this employeur from being saved:</h2>
<ul>
<% @employeur.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :siren, 'Siren: ' %><br>
<%= f.text_field :siren %>
</div>
<div class="field">
<%= f.label :societe, 'Société: ' %><br>
<%= f.text_field :societe %>
</div>
<div class="field">
<%= f.label :code_postal, 'Code Postal: ' %><br>
<%= f.text_field :code_postal %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我对rails非常陌生,因此,我看不出任何错误......提前感谢您的帮助。
由于评论,我正在添加我的模型和回溯:
class User < ActiveRecord::Base
has_one :prestataire
has_one :employeur
has_secure_password
end
class Employeur < ActiveRecord::Base
belongs_to :user
has_many :projets, as: :projetable
has_many :prestataires, through: :projets
has_many :offres, through: :projets
has_many :feedbacks, through: :projets
validates :siren, presence: true, uniqueness: true
validates_associated :users
end
开发日志文件:
[1m[36m (15.0ms)[0m [1mCREATE TABLE "offres" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "detail" text, "projet_id" integer, "prix" integer, "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (13.0ms)[0m CREATE TABLE "ouvriers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "siret" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime)
[1m[36m (17.0ms)[0m [1mCREATE INDEX "index_ouvriers_on_user_id" ON "ouvriers" ("user_id")[0m
[1m[35m (20.0ms)[0m CREATE TABLE "patrons" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "societe" varchar(255), "siret" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime)
[1m[36m (13.0ms)[0m [1mCREATE INDEX "index_patrons_on_user_id" ON "patrons" ("user_id")[0m
[1m[35m (14.0ms)[0m CREATE TABLE "projets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "titre" varchar(255), "description" text, "prix_haut" integer, "created_at" datetime, "updated_at" datetime)
[1m[36m (14.0ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "password_digest" varchar(255), "nom" varchar(255), "prenom" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (18.0ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
[1m[36m (9.0ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
[1m[35m (0.0ms)[0m SELECT version FROM "schema_migrations"
[1m[36m (9.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140427221811')[0m
[1m[35m (8.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131101095837')
[1m[36m (9.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131102103723')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103180052')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131103181116')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103181222')
[1m[36mActiveRecord::SchemaMigration Load (1.0ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35mActiveRecord::SchemaMigration Load (0.0ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (1.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (1.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)
[1m[35m (2.0ms)[0m rollback transaction
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (1.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (0.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)
[1m[35m (3.0ms)[0m rollback transaction
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (0.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (0.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)
[1m[35m (2.0ms)[0m rollback transaction
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (1.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (0.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)
[1m[35m (3.0ms)[0m rollback transaction
[1m[36m (1.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (1.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (0.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:21', '2014-04-28 22:38:21', 980190962)
[1m[35m (3.0ms)[0m rollback transaction
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (0.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (1.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:22', '2014-04-28 22:38:22', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:22', '2014-04-28 22:38:22', 980190962)
[1m[35m (2.0ms)[0m rollback transaction
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mFixture Delete (0.0ms)[0m DELETE FROM "offres"
[1m[36mFixture Insert (1.0ms)[0m [1mINSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:22', '2014-04-28 22:38:22', 980190962)[0m
SQLite3::SQLException: table offres has no column named references: INSERT INTO "offres" ("detail", "references", "created_at", "updated_at", "id") VALUES ('MyText', NULL, '2014-04-28 22:38:22', '2014-04-28 22:38:22', 980190962)
[1m[35m (2.0ms)[0m rollback transaction
[1m[36m (8.0ms)[0m [1mCREATE TABLE "employeurs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "societe" varchar(255), "siren" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime, "code_postal" varchar(255)) [0m
[1m[35m (5.0ms)[0m CREATE INDEX "index_employeurs_on_user_id" ON "employeurs" ("user_id")
[1m[36m (5.0ms)[0m [1mCREATE TABLE "feedbacks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "score" integer, "commentaire" text, "projet_id" integer, "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (4.0ms)[0m CREATE INDEX "index_feedbacks_on_projet_id" ON "feedbacks" ("projet_id")
[1m[36m (5.0ms)[0m [1mCREATE TABLE "offres" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "projet_id" integer, "price" decimal(8,2), "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (5.0ms)[0m CREATE INDEX "index_offres_on_projet_id" ON "offres" ("projet_id")
[1m[36m (5.0ms)[0m [1mCREATE TABLE "prestataires" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "siren" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (6.0ms)[0m CREATE INDEX "index_prestataires_on_user_id" ON "prestataires" ("user_id")
[1m[36m (7.0ms)[0m [1mCREATE TABLE "projets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "max_price" decimal(8,2), "created_at" datetime, "updated_at" datetime, "category" varchar(255), "delivery_date" time, "expiration_date" time, "projetable_id" integer, "projetable_type" varchar(255)) [0m
[1m[35m (7.0ms)[0m CREATE INDEX "index_projets_on_projetable_id_and_projetable_type" ON "projets" ("projetable_id", "projetable_type")
[1m[36m (7.0ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "password_digest" varchar(255), "surname" varchar(255), "forename" varchar(255), "created_at" datetime, "updated_at" datetime, "civility" varchar(255), "phone" varchar(255)) [0m
[1m[35m (7.0ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
[1m[36m (8.0ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
[1m[35m (1.0ms)[0m SELECT version FROM "schema_migrations"
[1m[36m (7.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140510205811')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131101095837')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131102103723')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103180052')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131103181116')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103181222')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140427221811')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140428232839')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140428235011')[0m
[1m[35m (34.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140428235712')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140429001342')[0m
[1m[35m (13.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504163617')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140504165702')[0m
[1m[35m (24.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504170115')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140504170335')[0m
[1m[35m (24.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504171748')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140507181545')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140510204225')
[1m[36mActiveRecord::SchemaMigration Load (1.0ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35mActiveRecord::SchemaMigration Load (1.0ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
[1m[36m (6.0ms)[0m [1mCREATE TABLE "employeurs" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "societe" varchar(255), "siren" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime, "code_postal" varchar(255)) [0m
[1m[35m (5.0ms)[0m CREATE INDEX "index_employeurs_on_user_id" ON "employeurs" ("user_id")
[1m[36m (6.0ms)[0m [1mCREATE TABLE "feedbacks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "score" integer, "commentaire" text, "projet_id" integer, "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (5.0ms)[0m CREATE INDEX "index_feedbacks_on_projet_id" ON "feedbacks" ("projet_id")
[1m[36m (5.0ms)[0m [1mCREATE TABLE "offres" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "projet_id" integer, "price" decimal(8,2), "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (6.0ms)[0m CREATE INDEX "index_offres_on_projet_id" ON "offres" ("projet_id")
[1m[36m (6.0ms)[0m [1mCREATE TABLE "prestataires" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "siren" varchar(255), "user_id" integer, "created_at" datetime, "updated_at" datetime) [0m
[1m[35m (7.0ms)[0m CREATE INDEX "index_prestataires_on_user_id" ON "prestataires" ("user_id")
[1m[36m (7.0ms)[0m [1mCREATE TABLE "projets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "max_price" decimal(8,2), "created_at" datetime, "updated_at" datetime, "category" varchar(255), "delivery_date" time, "expiration_date" time, "projetable_id" integer, "projetable_type" varchar(255)) [0m
[1m[35m (9.0ms)[0m CREATE INDEX "index_projets_on_projetable_id_and_projetable_type" ON "projets" ("projetable_id", "projetable_type")
[1m[36m (8.0ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "password_digest" varchar(255), "surname" varchar(255), "forename" varchar(255), "created_at" datetime, "updated_at" datetime, "civility" varchar(255), "phone" varchar(255)) [0m
[1m[35m (9.0ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
[1m[36m (8.0ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
[1m[35m (0.0ms)[0m SELECT version FROM "schema_migrations"
[1m[36m (8.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140510205811')[0m
[1m[35m (8.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131101095837')
[1m[36m (8.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131102103723')[0m
[1m[35m (8.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103180052')
[1m[36m (8.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20131103181116')[0m
[1m[35m (8.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20131103181222')
[1m[36m (13.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140427221811')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140428232839')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140428235011')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140428235712')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140429001342')[0m
[1m[35m (15.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504163617')
[1m[36m (27.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140504165702')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504170115')
[1m[36m (15.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140504170335')[0m
[1m[35m (27.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140504171748')
[1m[36m (14.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140507181545')[0m
[1m[35m (14.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140510204225')
[1m[36mActiveRecord::SchemaMigration Load (1.0ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35mActiveRecord::SchemaMigration Load (0.0ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"