2014-05-21 89 views
0

我正在嘗試創建一個將分兩部分完成的表單。首先,您指定用戶信息,然後在prestataire和employeur之間進行選擇,然後進行相應的重定向。在重定向到預警或工作表單之前,應保存用戶數據。我爲用戶創建了模型,視圖和控制器,我將向下添加,但是我目前有一個錯誤:參數數量錯誤(0代表1);我被卡住了。在此先感謝您的幫助。重定向和錯誤數量的參數(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) 

回答

2

你必須在該行一個錯字;你使用方括號而不是括號,所以它應該是這樣的params.require(:user)。不要忘記提到的@NickM錯字。

+0

有人倒了我的答案,所以我刪除了它。爲了記錄,我的答案中的拼寫錯誤在最後的params.require行中。你大寫了一個符號。趕上艾哈邁德。 –

+0

非常感謝你們! – Francky

相關問題