编辑路线不起作用红宝石

时间:2015-04-30 19:25:00

标签: ruby-on-rails ruby post routes

您正在尝试编辑/更新数据库中的某些数据,但我不断收到以下错误:

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编辑是:

  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:

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

1 个答案:

答案 0 :(得分:2)

表单以edit操作呈现,但会以update请求提交给PUT操作。您需要在控制器中实现它并在路径中处理它。

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