2012-04-19 4 views
0

항목을 만들고 있는데 특정 사용자의 것입니다. 내 differend 작업을하고 있었고 모든 것이 작동합니다 (항목은 올바른 사용자 ID, 이미지 업로드 등으로 생성됩니다).Edit (Rails 3.2)에서 라우팅 오류가 발생했습니다.

이제 모든 편집 작업을 위해 페이지가 렌더링되고 현재 항목 정보가 채워집니다. "업데이트"버튼을 클릭하면 라우팅 오류가 발생합니다. (어떤 경로 일치 [POST]는 "/ 상품/6")

내 컨트롤러

# 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 이미 표시 items_controller.rb 없다 사용자가 선택할 수있는 카테고리를 사용했습니다.

이제 내 edit.html.erb 파일이 (매우 긴 메신저 사용하여 부트 스트랩)

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

그리고 나는 그것이 모두 파일이라고 생각합니다. 마찬가지로 모든 것은 작동하지만 편집 버튼 만 누르면 편집 기능이 깨진 것 같습니다.

감사합니다.


편집

는 알려줘야 뒷조사 후 나는 문제를 발견했다. jquery를 사용하여 2 개의 입력 필드를 곱하는 숨겨진 입력을 업데이트합니다. 코드는 다음과 같습니다. 정말 나는 당신의 업데이트 버튼을 마크 업을 보지 못했다

// 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 # 업데이트 조치가 게시물에 응답하지 않으면 put에 응답합니다.

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

하지만 일반적으로 나던 레일이 자동으로이 물건을 수행

그래서 당신의 업데이트 버튼과 관련된 form_for는 다음과 같이 보일 필요가 있겠습니까? 마찬가지로 항목이 이미 있다는 것을 form_for에서 알 수 없으므로 새 항목을 만드는 대신 항목을 업데이트한다는 의미입니까? 버튼은 템플릿 파일의 하단에 있습니다 (<% = f.submit "항목 편집", 클래스 : "btn btn-large btn-primary"%>) 나는 또한 만들고 편집 할 수있는 사용자 모델을 가지고 있습니다. 이 사용자의 편집 양식은 내가이 줄을 필요로하지 않았다 ... 지금 왜? 답장을 보내 주셔서 감사합니다 :) – DanFritz

+0

편집 : 귀하의 솔루션을 tryed, 그리고 그것은 지금 같은 오류가 작동하지 않았다. (메서드 추가 : html 안에 "넣기": 바깥 쪽, 차이 없음). 어떤 아이디어? – DanFritz

+0

업데이트 버튼이있는보기 코드를 보겠습니다. 또한 양식 제출 요청에서 오류까지 포함하여 레일 콘솔 출력을 봅시다. – richardsun

관련 문제