Rails view 提交时,参数不知道参数哪里有问题,请帮看一下

davidminaki · 2015年04月08日 · 最后由 davidminaki 回复于 2015年04月08日 · 3113 次阅读

代码如下:

<%= form_for @product, url: {action: "create"}, html: {:class => 'form-horizontal'} do |f| %>

            <div class="row add-entity"  style="display:none;">
              <div>
                <div class="col-sm-2">
                  <%= f.label :product_category_id %>
                  <%= f.select :product_category_id, options_from_collection_for_select(ProductCategory.all, :id, :name) %>
                </div>
                <div class="col-sm-2">
                  <label>价格</label>
                  <input class="form-control" type="text" name="price">
                </div>
                <div class="col-sm-8">
                  <label>装备名称</label>
                  <input class="form-control" type="text" name="title">
                </div>
              </div>

              <h4 class="col-sm-12"><span class="brand"></span><span class="title"></span></h4>

              <div class="col-sm-12">
                <div class="thumbnail">
                  <img class="img-responsive entity-chief-img" src="">
                </div>
              </div>

              <div class="image-thumbnails"></div>

              <div>
                <%= hidden_field_tag(:buy_link, "") %>
                <%= hidden_field_tag(:user_id, "") %>
                <%= hidden_field_tag(:pcurl, "") %>
                <%= hidden_field_tag(:murl, "") %>
                <%= hidden_field_tag(:gid, "") %>
                <%= hidden_field_tag(:posturl, "") %>
                <%= hidden_field_tag(:mainpic, "") %>
                <%= hidden_field_tag(:piclist, "") %>
                <%= hidden_field_tag(:website, "") %>
              </div>
            </div>
            </div>
            <div class="row">
              <div class="media add-entity-note" style="display:none;">

                <div class="media-body">
                  <fieldset>
                    <div class="form-group">
                      <div class="col-sm-offset-1 col-sm-10">
                        <%= text_area_tag(:recommend, "来说说你的评价吧?欢迎真实、有用、有趣的商品点评。", :rows => 4, :class => 'form-control', :name => 'recommend') %>
                      </div>
                    </div>

                    <div class="form-group">
                      <div class="col-sm-offset-9 col-sm-3">
                        <%= f.submit "提交", class: 'btn btn-primary', data: {disable_with: "发表..."} %>
                      </div>
                    </div>
                  </fieldset>


                </div>
              </div>
            </div>
            </div>
        <% end %>

post 之后得到的参数是这样的

{"utf8"=>"✓", "authenticity_token"=>"6btCz9lvderZ+4Q8soQFe6bOxzuc/upD+ZAsx3dxnWKSMzp9MbhKR0tFvEI5lJOXHJ9bRMEovZCl6T+MIK7BBw==", "product"=>{"product_category_id"=>"1"}, "price"=>"53.90", "title"=>"美国aveenobaby婴儿儿童天然燕麦润肤乳宝宝面霜227g湿疹无激素", "buy_link"=>"", "user_id"=>"", "pcurl"=>"http://item.taobao.com/item.htm?id=16718391999", "murl"=>"http://h5.m.taobao.com/awp/core/detail.htm?id=16718391999", "gid"=>"", "posturl"=>"", "mainpic"=>"http://gw.alicdn.com/bao/uploaded/i3/TB1dSqxHXXXXXbGXVXXXXXXXXXX_!!0-item_pic.jpg", "piclist"=>"http://gw.alicdn.com/bao/uploaded/i3/TB1dSqxHXXXXXbGXVXXXXXXXXXX_!!0-item_pic.jpg,http://gw.alicdn.com/bao/uploaded/i4/654369049/T29vGVXy8aXXXXXXXX_!!654369049.jpg,http://gw.alicdn.com/bao/uploaded/i4/654369049/T2oUeVXDpXXXXXXXXX_!!654369049.jpg,http://gw.alicdn.com/bao/uploaded/i2/654369049/T2cryWXBJXXXXXXXXX_!!654369049.jpg", "website"=>"TB", "recommend"=>"来说说你的评价吧?欢迎真实、有用、有趣的商品点评。", "commit"=>"提交", "controller"=>"products", "action"=>"create"}

我希望得到参数是这样的

{"utf8"=>"✓", "authenticity_token"=>"6btCz9lvderZ+4Q8soQFe6bOxzuc/upD+ZAsx3dxnWKSMzp9MbhKR0tFvEI5lJOXHJ9bRMEovZCl6T+MIK7BBw==", "product"=>{"product_category_id"=>"1"}, "price"=>"53.90", "title"=>"美国aveenobaby婴儿儿童天然燕麦润肤乳宝宝面霜227g湿疹无激素", "buy_link"=>"", "user_id"=>"", "pcurl"=>"http://item.taobao.com/item.htm?id=16718391999", "murl"=>"http://h5.m.taobao.com/awp/core/detail.htm?id=16718391999", "gid"=>"", "posturl"=>"", "mainpic"=>"http://gw.alicdn.com/bao/uploaded/i3/TB1dSqxHXXXXXbGXVXXXXXXXXXX_!!0-item_pic.jpg", "piclist"=>"http://gw.alicdn.com/bao/uploaded/i3/TB1dSqxHXXXXXbGXVXXXXXXXXXX_!!0-item_pic.jpg,http://gw.alicdn.com/bao/uploaded/i4/654369049/T29vGVXy8aXXXXXXXX_!!654369049.jpg,http://gw.alicdn.com/bao/uploaded/i4/654369049/T2oUeVXDpXXXXXXXXX_!!654369049.jpg,http://gw.alicdn.com/bao/uploaded/i2/654369049/T2cryWXBJXXXXXXXXX_!!654369049.jpg", "website"=>"TB", "recommend"=>"来说说你的评价吧?欢迎真实、有用、有趣的商品点评。", "commit"=>"提交", "controller"=>"products", "action"=>"create"}

区别在

"product"=>{"product_category_id"=>"1"}, "price"=>"53.90", ..............

我希望

"product"=>{"product_category_id"=>"1" "price"=>"53.90", ..............},
<%= f.text_field :price, class: "form-control" %>

或者

<input class="form-control" type="text" name="product[price]"> 

两种方案

# 1
<input class="form-control" type="text" name="product['price']">

# 2
<%= f.text_field :price %>

#1 楼 @perish #2 楼 @flowerwrong

谢谢 但是 find("input[name=product[price]]").val(data.data.price); 在 jquery 里这样确实不行了

Uncaught Error: Syntax error, unrecognized expression: name=product[price]]

问题解决需要转义字符 addEntity.find("input[name=product\[price\]]").val(data.data.price);

需要 登录 后方可回复, 如果你还没有账号请 注册新账号