首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于#<UsersController:0x0000000570eef0>的未定义局部变量或方法“`user”

用于#<UsersController:0x0000000570eef0>的未定义局部变量或方法“`user”
EN

Stack Overflow用户
提问于 2014-05-22 19:01:45
回答 2查看 657关注 0票数 1

我对rails很陌生,我正试图从两部分创建一个表单。首先,填写用户表单。然后你选择成为一名预言家或雇员。通过单击相应的按钮,用户表单将被保存,您将被重定向到prestataire的表单或使用最近创建的用户params的员工的表单。

直到现在,我才设法发送最近创建的用户参数,只保存和重定向。但是,我在prestataire_controller中得到了这个错误,因为prestataire属于用户。当我单击employeur按钮时,错误类似:

undefined local variable or method 'user' for #<UsersController:0x0000000570eef0>

代码语言:javascript
运行
复制
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

下面是我到目前为止编写的代码:

用户表格:

代码语言:javascript
运行
复制
<%= 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 %>

用户控制器:

代码语言:javascript
运行
复制
  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

用户模型:

代码语言:javascript
运行
复制
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

路由文件:

代码语言:javascript
运行
复制
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不是最好的选择,但没有提出其他建议。提前谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-22 19:50:07

正如我说过的,错误是因为在create操作的这一行中它应该是@user

代码语言:javascript
运行
复制
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }

而且,您还没有在更新操作中初始化@user,这可能会在future.Add中出现另一个错误-在更新操作开始时,这一行

代码语言:javascript
运行
复制
@user = User.find(params[:id])
票数 1
EN

Stack Overflow用户

发布于 2014-05-22 19:28:02

您没有在更新时定义@user对象。尝试:

代码语言:javascript
运行
复制
  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
  end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23815149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档