2014-01-19 69 views
3

我試圖更新路由錯誤的路由匹配[PATCH]「/地點」

我使用before_action在此處設置的位置ID

before_action :set_location, only: [:show, :edit, :update, :destroy] 

private 
def set_location 
    @location = Location.find(params[:id]) 
end 

位置項之後得到一個路由錯誤是我的控制器 我只是有麻煩更新new和delete做工精細

def create 
@location = Location.new(location_params) 
respond_to do |format| 
    if @location.save 
    format.html { redirect_to @location, notice: 'Location was successfully created.' } 
    format.json { render action: 'show', status: :created, location: @location } 
    else 
    format.html { render action: 'new' } 
    format.json { render json: @location.errors, status: :unprocessable_entity } 
    end 
end 

def update 
respond_to do |format| 
    if @location.update(location_params) 
    format.html { redirect_to @location, notice: 'Location was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: 'edit' } 
    format.json { render json: @location.errors, status: :unprocessable_entity } 
    end 
end 
end 

這裏是如何我擊潰定義

Routes 

Routes match in priority from top to bottom 

Helper HTTP Verb Path Controller#Action 
Path/Url   
static_pages_home_path GET  /static_pages/home(.:format) static_pages#home 
static_pages_help_path GET  /static_pages/help(.:format) static_pages#help 
static_pages_about_path GET  /static_pages/about(.:format) static_pages#abo ut 
root_path     GET / static_pages#home 
locations_path   GET  /locations(.:format) locations#index 
          POST /locations(.:format) locations#create 
new_location_path   GET  /locations/new(.:format) locations#new 
edit_location_path   GET  /locations/:id/edit(.:format) locations#edit 
location_path    GET /locations/:id(.:format) locations#show 
          PATCH /locations/:id(.:format) locations#update 
          PUT  /locations/:id(.:format) locations#update 
          DELETE /locations/:id(.:format) locations#destroy 

不知道我是否需要添加別的

我想我可以添加微量

actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call' 
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged' 
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged' 
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged' 
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call' 
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call' 
rack (1.5.2) lib/rack/sendfile.rb:112:in `call' 
railties (4.0.2) lib/rails/engine.rb:511:in `call' 
railties (4.0.2) lib/rails/application.rb:97:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run' 
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread' 

_form文件

<%= form_for @location,:url => locations_path, :html => { :multipart => true , :class => 'form-horizontal' } do |f| %> 
    <div class="control-group"> 
    <%= f.label :latitude, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :latitude, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :longitude, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :longitude, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :address, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :address, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :description, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :description, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :title, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :title, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :user_id, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :user_id, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :rating, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :rating, :class => 'text_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :no_rates, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.text_field :no_rates, :class => 'text_field' %> 
    </div> 
    </div> 

    <%= f.file_field :picture %> 



    <div class="form-actions"> 
    <%= f.submit nil , :class => 'btn btn-primary' %> 
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
       locations_path, :class => 'btn' %> 
    </div> 


<% end %> 
+3

在你的路由定義你已經'PATCH /位置/:id'但你嘗試訪問'/ locations'。你的網址必須是'PATCH/locations/SOME_ID' – HungryCoder

+0

表單看起來如何?你有完整的堆棧跟蹤顯示嗎? – mestachs

+0

@HungryCoder我使用相同的代碼創造似乎工作正常。保存後視圖去/地點/編號 –

回答

12

您爲表單標記指定了目標網址,而不是讓Rails爲您選擇一個。正因爲如此,您的表單不會向PATH請求/locations/:id,而是嘗試向您指定的URL創建一個,該URL沒有附加PATCH路由。

只要從形式標記刪除:url選項,就像這樣:

<%= form_for @location, :html => { :multipart => true , :class => 'form-horizontal' } do |f| %>