0

我是 Ruby on Rails 的新手,在保存数据时遇到问题。所有项目都保存除了published,但我不知道为什么。

这是我的表格:

<% 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 %>

这是我的控制器:

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
4

1 回答 1

2

您需要将create操作添加到您的控制器,这应该是表单操作的终点:

def create
  @product = Product.new(params[:product])
  if @product.save
    # set flash and redirect to somewhere
  else
    # set flash.now with a short message
    # and render the form here again (render :new)
  end
end
于 2012-07-27T08:09:08.000 回答