2012-04-19 9 views
0

私はアイテムを作っており、それらは特定のユーザに属しています。私は自分の分化した行動をとっていて、すべてがうまくいっている(アイテムは正しいユーザーID、画像のアップロードなどで作られている)。Edit(Rails 3.2)でのルーティングエラー

編集アクションのすべてがうまくいき、ページがレンダリングされ、現在のアイテム情報が入力されます。「更新」ボタンをクリックすると、ルーティングエラーが発生します。 (Noルート一致[POST] "/商品/ 6")するコントローラitems_controller.rb

# items_controller.rb 
#------------------- 
class ItemsController < ApplicationController 
    before_filter :signed_in_user 
    respond_to :html, :js 

    def edit 
    @item = current_user.items.find(params[:id]) 
    @categorys = current_user.items.group(:category) 
    end 

    def update 
    if @item.update_attributes(params[:item]) 
     redirect_to items_path 
    else 
     render 'edit' 
    end 
    end 
end 

@item変数が現在のアイテムがIDを形成する取得

、その後も@categorysが既に表示されユーザーが選択できるカテゴリを使用します。

今私のedit.html.erbファイル(ブートストラップを使用して、その非常に長いIM)

<%= provide(:title, 'Edit Item') %> 
<h1>Edit Item</h1> 
<div class="row"> 
    <div class="span7"> 
    <%= form_for(@item, html: {class: "form-vertical"}) do |f| %> 
     <fieldset> 
      <span class="legend">Please pick a logo that represents the item.</span> 
      <%= render 'error_messages' %> 
      <div class="control-group"> 
      <label class="control-label" for="name">1. Give your item a name. Please don't include the volume of the 
       item.</label> 

      <div class="controls"> 
       <%= f.text_field :name, size: nil, class: 'input-xlarge', id: 'name' %> 
      </div> 
      </div> 

      <div class="control-group"> 
      <label class="control-label" for="logo">2. Select a Logo for the item (this will help identifying the 
       item)</label> 

      <div class="controls"> 
       <%= image_tag @item.logo.url(:original), alt: @item.name %> 
       <%= f.file_field :logo, size: nil, id: 'logo', 'accept' => 'image/*' %> 
      </div> 
      </div> 

      <div class="control-group"> 
      <label class="control-label" for="category">3. Type the name of a new category or click on an 
       existing</label> 
      <ul class="breadcrumb category-select"> 
       <% @categorys.each do |c| %> 
        <li><a href="#"><%= c.category %></a></li> 
       <% end %> 
      </ul> 
      <div class="controls"> 
       <%= f.text_field :category, size: nil, class: 'input-xlarge', id: 'category' %> 
      </div> 
      </div> 


      <div class="control-group"> 
      <label class="control-label">4.Specify how much 1 quantity is and how many items you have in stock at this 
       moment.<strong>Don't use this for updating the stock, please go to stock!</strong><br> 
       For example: a bottle of coke 0.25l and I have 24 of them.</label> 

      <div class="form-horizontal category-stock"> 
       <div class="control-group"> 
       <label class="control-label" for="quantity">Quantity (l)</label> 

       <div class="controls"> 
        <div class="input-append"> 
        <input class="span1" id="quantity" name="quantity" type="text"> 
        <span class="add-on">l</span> 
        </div> 
       </div> 
       </div> 
       <div class="control-group"> 
       <label class="control-label" for="amount">Amount (#)</label> 

       <div class="controls"> 
        <div class="input-append"> 
        <input class="span1" id="amount" name="amount" type="text"> 
        <span class="add-on">#</span> 
        </div> 
       </div> 

       </div> 
      </div> 

      <%= f.text_field :stock, size: nil, type: 'hidden' %> 

      <div class="form-actions category-stock"> 
       <%= f.submit "Edit Item", class: "btn btn-large btn-primary" %> 
      </div> 
      </div> 

     </fieldset> 


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

マイroutes.rbをファイル

root to: 'sessions#new' 

    resources :items do 
    member do 
     get :category 
    end 
    end 
    resources :events 

    resources :ei_relationships, only: [:create, :destroy] 

    resources :users 
    resources :sessions, only: [:new, :create, :destroy] 

    match '/signup', to: 'users#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 

私は私のサーバーで取得ログ:

Started GET "/items/6/edit" for 127.0.0.1 at 2012-04-20 00:34:34 +0200 
Processing by ItemsController#edit as HTML 
    Parameters: {"id"=>"6"} 
    User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '5le_BlKtwTQWeS15pzlCGQ' LIMIT 1 
    Item Load (0.0ms) SELECT "items".* FROM "items" WHERE "items"."user_id" = 1 AND "items"."id" = ? LIMIT 1 [["id", "6"]] 
    Rendered shared/_error_messages.html.erb (0.0ms) 
    Item Load (1.0ms) SELECT "items".* FROM "items" WHERE "items"."user_id" = 1 GROUP BY category 
    Rendered items/edit.html.erb within layouts/application (8.0ms) 
    Rendered layouts/_shim.html.erb (0.0ms) 
    Rendered layouts/_header.html.erb (1.0ms) 
    Rendered layouts/_footer.html.erb (1.0ms) 
Completed 200 OK in 77ms (Views: 73.0ms | ActiveRecord: 1.0ms) 


Started POST "/items/6" for 127.0.0.1 at 2012-04-20 00:34:47 +0200 

ActionController::RoutingError (No route matches [POST] "/items/6"): 
    actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.3) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.3) lib/rails/engine.rb:479:in `call' 
    railties (3.2.3) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 


    Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms) 

私はすべてのファイルだと思います。私はすべてが動作すると言ったように、編集ボタンが押されると編集機能だけが壊れているようです。

ありがとうございます!


私は問題を発見したいくつかの掘削後EDIT

オールライト。私は2つの入力フィールドの乗算である隠れた入力を更新するためにjqueryを使います。コードは以下の通りです。本当に

// Calculate the stock from the 2 fields and insert it in the hidden field 
     $('.category-stock').find('input').on("click select change", function() { 
      $.calculateStock(); 
     }); 
     $.calculateStock = function() { 
      var quantity = parseFloat($('input[name="quantity"]').val()); 
      var amount = parseFloat($('input[name="amount"]').val()); 

      var result = quantity * amount; 
      if (isNaN(result)) { 
       result = 0; 
      } 

      $('input[type="hidden"]').val(result); 
     }; 

答えて

0

...このコードは、ルーティングレールを台無しに理由を理解しない私はあなたの更新ボタンのマークアップを見ていないが、あなたは郵便を送ることによって、更新しようとしているかのように聞こえます。あなたのItemsController#updateアクションは投稿に応答せず、putに応答します。

<%= form_for(@item, html: {class: "form-vertical", method: "put"}) do |f| %> 
+0

が、通常doesntのレールが自動的にこのようなものを実行します。

は、だからあなたの更新ボタンに関連付けられているのform_forはこのような何かを見てする必要がありますか?それは、アイテムがすでにそこにあるということをform_forから知っているわけではないので、新しいアイテムを作る代わりにアイテムを更新することを意味します。ボタンはテンプレートファイルの下にあります(<%= f.submit "アイテムの編集"、クラス: "btn btn-large btn-primary"%>)私も作成して編集できるユーザーモデルを持っていますこのユーザーの編集フォーム私はこの行を必要としませんでした...なぜ今?あなたの返信ありがとう:) – DanFritz

+0

編集:あなたのソリューションを試して、それは今、同じエラーが仕事をdidnt。 (メソッドを追加:htmlの中に「put」:そして外側には、違いはありません)。何か案が? – DanFritz

+0

更新ボタンがあるビューコードを見てみましょう。フォーム提出リクエストからエラーまでのレールコンソールの出力を見てみましょう。 – richardsun