【问题标题】:Validate 2 models in 1 form在 1 个表单中验证 2 个模型
【发布时间】:2013-02-07 20:57:31
【问题描述】:

我正在学习 RoR,但我找不到以相同形式验证我的模型的方法。它正在验证,但它只显示一个模型(地址)的错误消息,并且它正在填充另一个模型的其他输入字段,就像在进行更改之前一样。我尝试了一些方法,但都没有奏效。

在 edit.html.erb 中,我显示了错误消息。仅当一个模型不正确时,此方法才能正常工作。如果两个模型中的输入都不正确,我只会看到地址的错误消息,并且相互性的输入字段被重置为以前的值。

我认为这就是你真正需要的。我的地址控制器是空的。除验证外,其他一切正常。

马蒂亚斯。

编辑.html.erb:

<h1>Edit Mutuality</h1>

<%= form_for @mutuality do |f| %>
<%= fields_for @address do |fa| %>
    <% if @mutuality.errors.any? %>
        <h2>Errors:</h2>
        <ul>
            <% @mutuality.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    <% end %>
    <% if @address.errors.any? %>
        <h2>Errors:</h2>
        <ul>
            <% @address.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    <% end %>
<table>
    <tr>
        <td>
            <%= f.label :name %>
        </td>
        <td>
            <%= f.text_field :name %>
        </td>
    </tr>
    <tr>
        <td>
            <%= f.label :phone %>
        </td>
        <td>
            <%= f.text_field :phone %>
        </td>
    </tr>
    <tr>
        <td>
            <%= f.label :contact %>
        </td>
        <td>
            <%= f.text_field :contact %>
        </td>
    </tr>
    <tr>
        <td>
            <%= fa.label :street %>
        </td>
        <td>
            <%= fa.text_field :street %>
        </td>
    </tr>
    <tr>
        <td>
            <%= fa.label :number %>
        </td>
        <td>
            <%= fa.text_field :number %>
        </td>
    </tr>
    <tr>
        <td>
            <%= fa.label :zipcode %>
        </td>
        <td>
            <%= fa.text_field :zipcode %>
        </td>
    </tr>
    <tr>
        <td>
            <%= fa.label :city %>
        </td>
        <td>
            <%= fa.text_field :city %>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
        <td>
            <%= f.submit %>
        </td>
    </tr>
</table>
<% end %>

控制器:

class MutualitiesController < ApplicationController

def index
    @mutualities = Mutuality.all
  end

  def show
    @mutuality = Mutuality.find(params[:id])
  end

  def new
    @mutuality = Mutuality.new
    @address = Address.new
  end

  def create
    @mutuality = Mutuality.new(params[:mutuality])
    @address = Address.new(params[:address])

    if @address.save
      @mutuality.address_id = @address.id
      if @mutuality.save
        redirect_to mutualities_path, :notice => "Mutuality saved with succes!"
      else
        render "new"
      end
    else
      render "new"
    end    
  end

  def edit
    @mutuality = Mutuality.find(params[:id])
    @address = Address.find(@mutuality.address_id)
  end

  def update
    @mutuality = Mutuality.find(params[:id])
    @address = Address.find(@mutuality.address_id)

    if @address.update_attributes(params[:address])
      if @mutuality.update_attributes(params[:mutuality])
        redirect_to mutualities_path, :notice => "Mutuality updated with succes!"
      else
        render "edit"
      end
    else
      render "edit"
    end

  end

  def destroy
    @mutuality = Mutuality.find(params[:id])
    @address = Address.find(@mutuality.address_id)
    @mutuality.destroy
    @address.destroy
    redirect_to mutualities_path
  end

end

【问题讨论】:

    标签: ruby ruby-on-rails-3 validation nested-forms


    【解决方案1】:

    Rails 可以自动处理嵌套属性。您正在手动完成所有操作。 不确定您是否在模型中正确定义了关联。

    所以,在你的模型中你应该写:

    class Mutuality
      belongs_to :address
      accepts_nested_attributes_for :address
    end
    

    然后在你的控制器中你可以写:

    def create
      @mutuality = Mutuality.new(params[:mutuality])
    
      if @mutuality.save
        redirect_to mutualities_path, :notice => "Mutuality saved with succes!"
      else
        render "new"
      end
    end    
    

    HTH。

    【讨论】:

      【解决方案2】:

      我终于成功了!我做了 Nathanvda 告诉我的更改,但我必须添加一些内容才能使其正常工作。还有一个小问题。我无法删除地址。它只会删除互惠。有什么想法吗?

      地址.rb

      class Address < ActiveRecord::Base
        #attr_accessible :street, :number, :zipcode, :city
        attr_protected :id
      
        has_one :mutuality
      
        validates_presence_of :street, :number, :city
      
      end
      

      Mutuality.rb

      class Mutuality < ActiveRecord::Base
        #attr_accessible :name, :phone, :contact, :address_id
        attr_protected :id, :address_id
      
        belongs_to :address
        accepts_nested_attributes_for :address, :allow_destroy => true
      
        validates_presence_of :name, :phone, :contact
        validates_length_of :name, :minimum => 5
        validates_uniqueness_of :name
      
      end
      

      这是我在 show.html.erb 上使用的链接:

      <p>
          <%= link_to "Back", mutualities_path %> |
          <%= link_to "Edit", edit_mutuality_path %> |
          <%= link_to "Delete", @mutuality, :confirm => "You are going to delete " +  @mutuality.name + ". Do you wish to continue?", :method => :delete %>
      </p>
      

      这里是销毁函数:

        def destroy
          @mutuality = Mutuality.find(params[:id])
          if @mutuality.destroy
            redirect_to mutualities_path, :notice => "Mutuality deleted with succes!"
          else
             redirect_to mutualities_path, :error => "A problem has occured."
          end
        end
      

      马蒂亚斯

      【讨论】:

        猜你喜欢
        • 2013-01-11
        • 2017-06-14
        • 2015-03-09
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 2017-08-26
        • 2020-08-08
        • 2013-03-17
        相关资源
        最近更新 更多