Rails 4:嵌套的ressource复数是一种未定义的方法

时间:2014-05-25 17:56:32

标签: ruby-on-rails forms models controllers

我整个星期都在研究这个问题,尽管很多博客都在处理这个问题,比如this,但我遇到了错误:

undefined method `employeurs'... 

让我们假设您有两种特定类型的用户,即雇员和prestataires。首先,您使用一般信息(姓名,电子邮件等)填写用户表单。然后,您可以在prestataire或employeur表单之间进行选择,这将保存用户表单并将您重定向到题字的第二部分。雇主和prestataires模型都与用户有属于关系。

使用下面的代码,我已经能够创建并保存用户表单,被重定向到雇主或prestataire表单,创建一个employeeur。然而,当我在employeeur控制器中使用@user.employeurs时,就像在我的索引方法中一样,我收到一个错误:

undefined method `employeurs'

尽管我有嵌套路线:

resources :users do
  resources :employeurs
  resources :prestataires
end 

用户模型:

class User < ActiveRecord::Base
  has_one :prestataire
  has_one :employeur

  has_secure_password
end

雇主模特:

class Employeur < ActiveRecord::Base
  belongs_to :user
  has_many :projets, as: :projetable
  has_many :prestataires, through: :projets
  has_many :offres, through: :projets
  has_many :feedbacks, through: :projets
  validates :siren, :societe, :code_postal, presence: true
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
    @user = User.find(params[:id])
  end

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

    respond_to do |format|
      if @user.save
        if params[:commit] == 'Prestataire'
        format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations d'employeur" }
        format.json { render action: 'show', status: :created, location: @user }
        else 
        format.html { redirect_to new_user_employeur_path(user_id: @user), 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
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update(user_params)
        if params[:commit] == 'Prestataire'
        format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: 'User was successfully updated.' }
        format.json { head :no_content }
        else 
        format.html { redirect_to new_user_employeur_path(user_id: @user), 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

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @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)
  end

end

雇主控制员:

class EmployeursController < ApplicationController
  before_filter :load_user

  def index
    @employeurs = @user.employeurs.all
  end


  def show
    @employeur = Employeur.find(params[:id])
  end

  def new
    @employeur = @user.build_employeur
  end

  def edit
    @employeur = Employeur.find(params[:id])
  end


  def create
    @employeur = @user.build_employeur(employeur_params)
    respond_to do |format|
      if @employeur.save
        format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
        format.json { render action: 'show', status: :created, location: @employeur }
      else
        format.html { render action: 'new' }
        format.json { render json: @employeur.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
  @employeur = Employeur.find(params[:id])
    respond_to do |format|
      if @employeur.update_attributes(employeur_params)
        format.html { redirect_to [@user, @employeur], notice: 'Employeur was successfully created.' }
        format.json { render action: 'show', status: :created, location: @employeur }
      else
        format.html { render action: 'new' }
        format.json { render json: @employeur.errors, status: :unprocessable_entity }
      end
    end
  end


def destroy
  @employeur = Employeur.find(params[:id])
  @employeur.destroy
  respond_to do |format|
    format.html { redirect_to @user }
    format.json { head :no_content }
  end
end

private
  def load_user
    @user = User.find(params[:user_id])
  end

  def employeur_params
    params.require(:employeur).permit(:siren, :societe, :code_postal)
  end

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" %>
    <%= f.submit "Prestataire" %>
  </div>
<% end %>

雇主表格:

<%= form_for [@user, @employeur] do |f| %>
  <% if @employeur.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@employeur.errors.count, "error") %> prohibited this employeur from being saved:</h2>

      <ul>
      <% @employeur.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :siren, 'Siren: ' %><br>
    <%= f.text_field :siren %>
  </div>
  <div class="field">
    <%= f.label :societe, 'Société: ' %><br>
    <%= f.text_field :societe %>
  </div>
  <div class="field">
    <%= f.label :code_postal, 'Code Postal: ' %><br>
    <%= f.text_field :code_postal %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

雇主索引视图:

<h1>Liste des employeurs</h1>
<ul>
<% @employeurs.each do |employeur| %>
  <li><%= @user.employeur.siren %> | <%= @user.employeur.societe %> <%= @user.employeur.code_postal %> <%= link_to "++", user_employeur_path(employeur) %></li>
<% end %>
</ul>

由于我不明白这个错误可能来自何处,我可能忘记添加一些有用的代码部分。不要犹豫。提前谢谢。

2 个答案:

答案 0 :(得分:0)

你混淆了路线和模型。嵌套路线对模型的组织没有任何影响。

resources :users do
  resources :employeurs
  resources :prestataires
end

这些路由具有将特定URL请求连接到控制器方法的效果。他们还设置了辅助方法,如users_path等。

你宣布你的模型是这样的:

class User < ActiveRecord::Base
  has_one :employeur
end

因此User只有一个Employeur,为什么您希望它有多个?{/ p>

我建议您重新考虑整个模型结构。我觉得EmployeurPrestataire实际上只是User的子类型,而不是他们自己的类型。

答案 1 :(得分:0)

首先,对于has_one关联,您应该使用 Singular Resource

resources :users do
resource :employeur #here
resource :prestataire #and here
end 

其次,您应该将其称为@user.employeur而不是@user.employeurs,因为它与 has_one users 关系但是不是 has_many