Rails简单表单 - 没有路由匹配

时间:2016-07-16 10:47:51

标签: ruby-on-rails simple-form

每个用户都可以创建一些博客,当他们登录时,会看到他们的博客列表和旁边的按钮,如下所示:

= simple_form_for activate_blog_path(blog.id), method: :put do |f|
  = hidden_field_tag :active, value: true
  = f.button :submit

即使路径中存在路径,我仍然收到此错误消息:

No route matches [PUT] "/"

routes.rb中:

resources :users
resources :blogs do
  member do
    get :activate
    put :activate
  end
end
root 'pages#index'

rails routes:

       Prefix Verb   URI Pattern                    Controller#Action
        users GET    /users(.:format)               users#index
              POST   /users(.:format)               users#create
     new_user GET    /users/new(.:format)           users#new
    edit_user GET    /users/:id/edit(.:format)      users#edit
         user GET    /users/:id(.:format)           users#show
              PATCH  /users/:id(.:format)           users#update
              PUT    /users/:id(.:format)           users#update
              DELETE /users/:id(.:format)           users#destroy
activate_blog GET    /blogs/:id/activate(.:format)  blogs#activate
              PUT    /blogs/:id/activate(.:format)  blogs#activate
        blogs GET    /blogs(.:format)               blogs#index
              POST   /blogs(.:format)               blogs#create
     new_blog GET    /blogs/new(.:format)           blogs#new
    edit_blog GET    /blogs/:id/edit(.:format)      blogs#edit
         blog GET    /blogs/:id(.:format)           blogs#show
              PATCH  /blogs/:id(.:format)           blogs#update
              PUT    /blogs/:id(.:format)           blogs#update
              DELETE /blogs/:id(.:format)           blogs#destroy
         root GET    /                              pages#index

blogs_controller.rb:

  def activate
    @blog.active = true
    @blog.save
    redirect_to root_path
  end

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

simple_form_for期望一个对象(或记录)生成表单标记。要将自定义网址和方法传递给表单操作,您可以使用

= simple_form_for blog, url: activate_blog_path(blog.id), method: :put do |f|
  = hidden_field_tag :active, value: true
  = f.button :submit

答案 1 :(得分:-1)

您需要指定操作和控制器以及方法

= simple_form_for blog, url: url_for(action: :activate, controller: 'blogs'), method: :put do |f|