【发布时间】:2014-05-22 20:51:13
【问题描述】:
我是 Rails 新手,我正在尝试创建一个分为两部分的表单。首先,您填写用户表单。然后你选择成为一名prestataire 或employeur。通过单击相应的按钮,用户表单将被保存,您将被重定向到具有最近创建的用户参数的 prestataire 表单或员工表单。
直到现在,我还没有设法发送最近创建的用户参数,只是保存和重定向。但是后来我在 prestataire_controller 中收到了这个错误,因为 prestataire 属于用户。当我点击employeur按钮时,错误类似:
def new
@user = User.find(params[:user_id]) #This is where there's an error.
@prestataire = @user.build_prestataire
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="field">
<%= f.label :forename, 'Prénom: ' %><br>
<%= f.text_field :forename %>
</div>
<div class="field">
<%= f.label :surname, 'Nom de famille: ' %><br>
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :email, 'Email: ' %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password, 'Mot de passe: ' %><br>
<%= f.password_field :password, size: 40 %>
</div>
<div class="field">
<%= f.label :password_confirmation, 'Confirmation de mot de passe: ' %><br>
<%= f.password_field :password_confirmation, size: 40 %>
</div>
<div class="field">
<%= f.label :phone, 'Numéro de téléphone: ' %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit 'Employeur', name: 'employeur' %>
<%= f.submit 'Prestataire', name: 'user_id' %>
</div>
<% end %>
用户控制器:
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
if params[:commit] == 'employeur'
format.html { redirect_to new_employeur_path, notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_prestataire_path, 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
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to new_employeur_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone, :employeur)
end
用户模型:
class User < ActiveRecord::Base
has_one :prestataire
has_one :employeur
validates :email, presence: true, uniqueness: true
validates :password, :forename, :surname, :phone, :civility, presence: true
validates :password, confirmation: true
has_secure_password
end
Prestataire 模型:
class Prestataire < ActiveRecord::Base
belongs_to :user
has_many :projets, as: :projetable
has_many :employeurs, through: :projets
has_many :offres, through: :projets
has_many :feedbacks, through: :projets
validates :siren, presence: true, uniqueness: true
validates_associated :users
end
路线文件:
Workplace::Application.routes.draw do
resources :users
resources :prestataires
resources :employeurs
resources :projets
resources :feedbacks
resources :offres
root 'projets#index'
我正在考虑在 f.submit 上添加一个 onclik 选项并添加一个适当的方法。但是我在某处读到 f.submit 不是这种请求的最佳选择,但没有提出其他建议。提前感谢您的帮助。
【问题讨论】:
-
我们能看到错误吗?如果它与路由有关,那么这些控制器的路由文件。
-
也许我应该添加一个 CurrentUser 模块并在使用 f.submit 加载到数据库之前注册用户信息?...
-
错误是:找不到没有ID的用户
-
我认为你的方法很好。将信息保存到用户中,然后创建角色(prestatair 或employeur)非常棒!请参阅我的答案以获取有关以这种方式工作的帮助。
标签: ruby-on-rails forms