使用嵌套资源处理has_one和singular资源

时间:2014-05-27 20:22:24

标签: ruby-on-rails routes nested has-one

我有一个用户模型,其中有一个prestataire和has_one employeur。以前在stackoverflow上,有人建议我声明奇异的资源,如:

resources :users do
resource: employeur
resource: prestataire
end

而不是:

resources :users do
resources: employeurs
resources: prestataires
end

感谢rails,我没有必要将所有控制器和视图名称文件单独化。然而,当我创建一个用户并被重定向到employeeur表单时,我得到了未定义的方法`user_employeurs_path',这是正确的,因为我只有一个user_employeur_path。但我没有在我的用户控制器中要求复数。 Rails表示这个NoMethodError发生在我的雇员表格的第一行<%= form_for [@ user,@ employee]] | f | %>,其中一个在保存用户时被重定向。

class UsersController < ApplicationController

#TODO index user doit être suprimé quand inutile pour dev
  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

用户表单:

<%= 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>New employeur</h1>
<%= render 'form' %>

2 个答案:

答案 0 :(得分:2)

以下是发生的事情:

当您传递<%= form_for [@user, @employeur] do |f| %>时,Rails会按照您传递的顺序连接对象(@user&amp; @employeur),然后将path附加到其中。所以这变为user_employeurs_path。按照惯例,它将使最后一个对象(employeur)复数化,以推断出控制器的名称。它是如何得到的:user_employeurs_path

因此您必须将控制器的资源复数化以遵循惯例。或者将URL传递到您的路径:

<%= form_for [@user, @employeur],  url: user_employeur_path do |f| %>

答案 1 :(得分:1)

我相信在这种情况下您可以使用url参数。尝试类似:

<%= form_for [@user, @employeur], url: user_employeur_path do |f| %>
   ...