重定向和错误的参数数量(0表示1)

时间:2014-05-21 18:52:58

标签: ruby-on-rails

我正在尝试创建一个将分为两部分完成的表单。首先,您指明用户信息,然后在prestataire和employeur之间进行选择,并相应地重定向。在重定向到prestataire或employeur表单之前,应保存用户数据。我已经为用户创建了模型,视图和控制器,我将向下添加,但我现在有错误:参数数量错误(0表示1);而且我被困住了。在此先感谢您的帮助。

型号:

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

查看:

    <%= 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 %>

控制器:

 class UsersController < ApplicationController
      def index
        @users = User.all
      end

      def show
        @user = User.find(params[:id])
      end

      def new
        @user = User.new
      end

      # GET /users/1/edit
      def edit
      end

      # POST /users
      # POST /users.json
      def create
        @user = User.new(user_params)

        respond_to do |format|
          if @user.save
            #if params[:commit] == 'Employeur'
            format.html { redirect_to @employeur, notice: "Renseignez vos informations d'employeur" }
            format.json { render action: 'show', status: :created, location: @user }
            #else 
            #format.html { redirect_to @prestataire, 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 @user, 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

      # DELETE /users/1
      # DELETE /users/1.json
      def destroy
        @user.destroy
        respond_to do |format|
          format.html { redirect_to users_url }
          format.json { head :no_content }
        end
      end

    private
      def user_params
            params.require[:user].permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone, :Employeur)
      end

    end

我的路线只是以防万一:

Workplace::Application.routes.draw do
  resources :users
  resources :prestataires
  resources :employeurs
  resources :projets
  resources :feedbacks
  resources :offres
  root 'projets#index'

错误被识别为位于此行的控制器部分:

params.require[:user].permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone, :Employeur)

1 个答案:

答案 0 :(得分:2)

你在那条线上有一个错字;你使用方括号而不是括号,所以它应该像params.require(:user)一样。不要忘记提到的错误@NickM。