2015-04-30 38 views
1

你好我試圖在數據庫中編輯/更新一些數據,但我不斷收到以下錯誤:路線的編輯工作不紅寶石

No route matches [POST] "/books/11/edit" 

我嘗試添加一些不同的線routes.rb中如

post 'books/edit' 

等..但我沒有運氣。這是我目前的Routes文件。

Rails.application.routes.draw do 

    resources :books, :only => [:new, :create, :edit, :index] 

    resources :details, :only => [:new, :edit] 

    resources :members 

    devise_for :users 

    get 'page/books' 

    get 'page/about' 

    get 'page/contact' 

    get 'page/home' 

    post "details/new" 

    # The priority is based upon order of creation: first created -> highest priority. 
    # See how all your routes lay out with "rake routes". 

    # You can have the root of your site routed with "root" 
    root 'page#home' 

我的防守編輯是:

def edit 
    @book = Book.find(params[:id]) 
    end 

和表單正在使用所用的是:

<h4> Book Details </h4> 

<p> Edit This Current Book </p> 
<%= form_for @book do |f| -%> 
<div class="field"> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :description %><br /> 
    <%= f.text_field :description, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :isbn_number %><br /> 
    <%= f.text_field :isbn_number, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :author %><br /> 
    <%= f.text_field :author, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :status %><br /> 
    <%= f.text_field :status, autofocus: true %> 
    </div> 

    <br /> 
    <%= f.submit "Edit Book" %> 
<% end -%> 

    <br /> 

==加入這個== 防守更新後曾任職:

def update 
    @book = Book.find(params[:id]) 
    @book.update_attributes(book_params) 
    redirect_to new_book_path 
    end 
+0

什麼時候出現這個錯誤?是否當你嘗試提交表單? – mikej

+0

我無法進入表單頁面,只要我點擊編輯應該鏈接到編輯頁面發生錯誤 – Roland

+0

好吧,聽起來就像編輯鏈接的問題。這應該是生成GET請求而不是POST。你是否在用'link_to'編輯',edit_book_path(book)'? – mikej

回答

2

表單呈現w ith edit行動,但它提交到update行動作爲PUT請求。你需要在你的控制器中實現它並在路由中處理它。

resources :books, :only => [:new, :create, :edit, :update, :index] 
+0

我在想,即使有更新操作,我也應該至少能夠查看錶單隻有當我嘗試提交應該失敗,因爲更新尚未實現,但我會嘗試添加它現在看到 – Roland

+0

我添加了更新操作,現在它的作品現在謝謝你:D! – Roland