我对rails很陌生,我正试图从两部分创建一个表单。首先,填写用户表单。然后你选择成为一名预言家或雇员。通过单击相应的按钮,用户表单将被保存,您将被重定向到prestataire的表单或使用最近创建的用户params的员工的表单。
直到现在,我才设法发送最近创建的用户参数,只保存和重定向。但是,我在prestataire_controller中得到了这个错误,因为prestataire属于用户。当我单击employeur按钮时,错误类似:
undefined local variable or method 'user' for #<UsersController:0x0000000570eef0>
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_user_prestataire_path(user_id: user), notice: "Renseignez vos informations de prestataire" } #This is sublined
format.json { render action: 'show', status: :created, location: @user }
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: 'prestataire' %>
</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_user_employeur_path, notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_user_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)
if params[:commit] == 'employeur'
format.html { redirect_to new_user_employeur_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_prestataire_path, notice: "User was successfully updated." }
format.json { head :no_content }
end
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路由文件:
Workplace::Application.routes.draw do
resources :users do
resources :prestataires
resources :employeurs
end
resources :projets do
resources :feedbacks
resources :offres
end
root 'projets#index'我正在考虑在f.submit上添加一个onclik选项,并添加一个适当的方法。但我在某个地方读到,对于这种请求,f.submit不是最好的选择,但没有提出其他建议。提前谢谢你的帮助。
发布于 2014-05-22 19:50:07
正如我说过的,错误是因为在create操作的这一行中它应该是@user
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }而且,您还没有在更新操作中初始化@user,这可能会在future.Add中出现另一个错误-在更新操作开始时,这一行
@user = User.find(params[:id])发布于 2014-05-22 19:28:02
您没有在更新时定义@user对象。尝试:
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update(user_params)
if params[:commit] == 'employeur'
format.html { redirect_to new_user_employeur_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_prestataire_path, notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
endhttps://stackoverflow.com/questions/23815149
复制相似问题