产品验证

时间:2012-07-27 03:29:20

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

大家好我是铁轨上的红宝石新手...我添加了产品页面,如果1类别下有重复的产品名称,例如“产品名称:测试”,“类别:其他”,则验证“产品”名称已经存在请选择另一个“。但如果您更改类别,例如“产品名称:测试”,“类别:购物和掉落”,它将保存新产品。

这是我的表单中的代码:添加新产品

<% content_for :post_content do %>
    <div class="post">
        <% form_for :product, @product do | fld | %>
        <span class="notice"><% if @product.errors.any? %><p class="error"><%= @product.errors.first[1] %></p><% end %></span>
        <table class="tbl-form" cellpadding="0" cellspacing="0">
            <tbody>
                <tr>
                    <td class="name" width="100">Code:</td>
                    <td colspan="99"><%= fld.text_field :product_code, :class => "large" %></td>
                </tr>
                <tr height="5"/>
                <tr>
                    <td class="name" width="100">Name:</td>
                    <td colspan="99"><%= fld.text_field :name, :class => "large" %></td>
                </tr>
                <tr height="5"/>
                <tr>
                    <td class="name" width="100">Start Week:</td>
                    <td colspan="99"><%= fld.select :start_week, options_for_select(StockMovement.order("year DESC, week DESC").map { | val | [ "#{ val.year }/#{ val.week }", val.id] }, :selected => @product.start_week), :class => "ddl_SW" %></td>
                </tr>
                <tr height="5"/>
                <tr>
                    <td class="name" width="100">Category:</td>
                    <td colspan="99"><%= fld.select :product_category, options_for_select(ProductCategory.where("jos_product_category.published = 1").all.map { | val | [ val.name, val.id] }, :selected => @product.product_category)%></td>
                </tr>
                <tr height="5"/>
                <tr>
                    <td class="name">Thumbnail:</td>
                    <td colspan="99"><%= fld.text_field :thumbnail, :class => "large" %></td>
                </tr>
                <tr height="5"/>
                <tr>
                    <td class="name">Original Image:</td>
                    <td colspan="99"><%= fld.text_field :original_image, :class => "large" %></td>
                </tr>
                <tr height="5"/>
                <tr><td class="name">Publish:</td><td><span id="yesno"><%= fld.check_box :published, :class => "hide-chk" %><a id="true" alt="1" rel="product_published" class="yes">Yes</a><a id="false" alt="0" rel="product_published" class="no on">No</a></span></td></tr>
                <tr height="25"/>
                <tr class="btn-holder">
                    <td colspan="99">
                        <input type="image" src="/images/btn-save.png" class="img-btn"><a href="<%= admin_products_path %>" class="lnk-btn back">Back</a>
                    </td>
                </tr>
                <tr height="5"/>
            </tbody>
        </table>
        <% end %>
    </div>
<% end %>

这是我的模特:

 validates_uniqueness_of :product_code, :message => 'Product code is already taken'
  validates_presence_of :product_code, :message => "Product code is required"
  validates_presence_of :name, :message => "Product name is required"
  validates_uniqueness_of :name, :message => 'Product name is already taken'

这是在我的控制器中:

def new
    @product = Product.new

    if request.post? and params[:product]
      @product = Product.new(params[:product])
      @product.creator = logged_user['clientID']

      if @product.save
        #render :json => params[:product]
        redirect_to admin_product_show_url(:productID => @product.id), :notice => '<p class="success">You have successfully added a new product '"#{ @product.name }"'</p>'
      end
    end
  end

注意:产品和类别是与数据库不同的表....

提前感谢..

1 个答案:

答案 0 :(得分:0)

我假设您必须在产品表中为类别表

提供类似category_id的引用键

所以改变这个

 validates_uniqueness_of :name, :message => 'Product code is already taken'

 validates_uniqueness_of :name, :scope => category_id, :message => 'Product code is already taken'

这将验证特定类别

的产品名称的唯一性