Update方法以奇怪的方式传递id

时间:2016-11-08 10:41:22

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

不知何故,编辑后的更新方法是将id传递为“show”

这里是我“更新”\

时传递的参数
Started PATCH "/owners/show.3328" for 127.0.0.1 at 2016-11-08 12:28:29 +0200
Processing by OwnersController#update as 
  Parameters: {"utf8"=>"✓", "owner"=>{"name"=>"Kamal Ghool", "phone"=>"05222123123", "email"=>"kamal057@gmail.com", "notes"=>"", "customer_id"=>"", "phone2"=>"", "address1"=>"Omar ben khattab St", "city"=>"Umm el fahem", "zipcode"=>"30010"}, "commit"=>"עדכון לקוח", "id"=>"show"}
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Shop Load (0.3ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Owner Load (0.5ms)  SELECT  "owners".* FROM "owners" WHERE "owners"."shop_id" = $1 AND "owners"."id" = $2 LIMIT $3  [["shop_id", 1], ["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 31ms (ActiveRecord: 1.7ms)
ActiveRecord::RecordNotFound (Couldn't find Owner with 'id'=show [WHERE "owners"."shop_id" = $1]):

我的表单是新主人(和作品)的相同形式:

<%=form_for @owner , remote: true do |f| %>

    <div class="modal-body">
    <div class="row">

        <div>

            <div class="col-md-6" style="float: right">
                <div class="form-group">

        <%#= f.hidden_field :owner_id, { :value => @owner.id } %>

                <%= f.label 'שם לקוח', class:"control-label" %>
                <%= f.text_field :name, class: "form-control" %>
                </div>

                <div class="form-group">
                <%= f.label 'טלפון לקוח', class: "control-label" %>
                <%= f.text_field :phone, class: "form-control" %>
                </div>
                <div class="form-group">

                <%= f.label 'דוא"ל', class:"control-label" %>
                <%= f.text_field :email, class: "form-control" %>
                </div>
                <div class="form-group">
                <%= f.label 'הערות ללקוח', class: "control-label" %>
                <%= f.text_field :notes, class: "form-control" %>
                </div>
            </div>
            <div class="col-md-6">
      <div class="form-group">
            <%= f.label 'ת"ז', class:"control-label" %>
            <%= f.text_field :customer_id, class: "form-control" %>
        </div>     
        <div class="form-group">
            <%= f.label 'טלפון נוסף', class: "control-label" %>
            <%= f.text_field :phone2, class: "form-control" %>
      </div>
      <div class="form-group">
            <%= f.label 'כתובת', class:"control-label" %>
            <%= f.text_field :address1, class: "form-control" %>
        </div>         
      <div class="row">
      <div class="form-group col-xs-6">
            <%= f.label 'עיר', class:"control-label" %>
            <%= f.text_field :city, class: "form-control col-xs2" %>
        </div>  
      <div class="form-group col-xs-6">
                        <%= f.label 'מיקוד', class:"control-label" %>
              <%= f.text_field :zipcode, class: "form-control col-xs2" %>

     </div>         
     </div>           

            </div>
        </div>

    </div>

    </div>

    <div class="modal-footer">
          <%= f.submit class: "btn btn-primary" %>

                <%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>
    </div>
  

update.html.erb呈现相同的save.js.erb,它在创建新所有者时可以找到。

2 个答案:

答案 0 :(得分:2)

在rails中创建RESTful路由的首选方法是使用资源:

resources :owners

哪个会给你GET /owners/:id这是一个规范的Rails REST路由,它指向OwnersController上的show动作。

不要创建以下路线:

/owners/show
/owners/index
/owners/create

除非你想看起来不称职。在Rails中,操作由所使用的HTTP谓词和最后存在的动态ID段推断。

请参阅:

答案 1 :(得分:0)

发布问题后3秒,我在路线文件中找到了这个:

  get 'owners/show'#, as: :owner

并将其更改为

  get 'owners/show'

看起来我不需要那个别名。