form_for helper给我一个错误,尽管那里缺少一个参数

时间:2018-09-16 11:44:22

标签: ruby-on-rails

我在Rails中的form_for帮助器有问题。我想在论坛应用中编辑Post

尝试调用编辑页面时出现此错误:

ActionView::Template::Error (No route matches 
{:action=>"show", 
 :category=>#<Post id: 1, 
                   user_id: 1, 
                   title: "Die Webseite ist nun online", 
                   body: "<div>Viel Spaß euch allen. Und haltet euch an die ...", 
                   category: "general/announcements", 
                   slug: "die-webseite-ist-nun-online", 
                   created_at: "2018-09-16 01:00:55", 
                   updated_at: "2018-09-16 01:00:55">, 
 :controller=>"posts", 
 :id=>"die-webseite-ist-nun-online", 
 :locale=>:de}, 

possible unmatched constraints: [:category]):

这是我的Edit动作:

def edit
    @post = Post.find(params[:id])
end

这是对form_for

的呼叫
<%= form_for(@post) do |f| %>

我尝试过的事情:

<%= form_for(@post, category: params[:category]) do |f| %>

params[:category]具有正确的值,我测试了它是否为空或nil

提供这样的category参数并不能改变我得到的错误!

编辑:

根据我的routes.rb要求:

Rails.application.routes.draw do
  concern :paginatable do
    get '(page/:page)', action: :index, on: :collection, as: ''
  end
  CATEGORY_FILTERS = /(?x)general\/suggestions|general\/member-introductions|
                           general\/announcements|off-topic\/jobs-and-projects|
                           off-topic\/miscellaneous|off-topic\/funny-stuff|
                           ruby-on-rails\/news|ruby-on-rails\/developers|
                           ruby-on-rails\/tutorials/
  scope '(:locale)', locale: /en|de/ do
    root 'forum#home'
    get    '/help',    to: 'forum#help'
    get    '/about',   to: 'forum#about'
    get    '/contact', to: 'forum#contact'
    get    '/general', to: 'forum#general'
    get    '/ruby-on-rails', to: 'forum#rubyonrails'
    get    '/off-topic', to: 'forum#offtopic'
    get    '/signup', to: 'users#new'
    get    '/login',   to: 'sessions#new'
    post   '/login',   to: 'sessions#create'
    delete '/logout',  to: 'sessions#destroy'
    resources :users, concerns: :paginatable
    resources :password_resets, only: %i[new edit create update]
    scope '*category', category: CATEGORY_FILTERS do
      get '', to: 'posts#index'
      resources :posts, concerns: :paginatable
    end
  end
  resources :account_activations, only: [:edit]
end

路线给出​​的路线

Prefix Verb   URI Pattern                                                                              Controller#Action
                     root GET    /(:locale)(.:format)                                                                     forum#home {:locale=>/en|de/}
                     help GET    (/:locale)/help(.:format)                                                                forum#help {:locale=>/en|de/}
                    about GET    (/:locale)/about(.:format)                                                               forum#about {:locale=>/en|de/}
                  contact GET    (/:locale)/contact(.:format)                                                             forum#contact {:locale=>/en|de/}
                  general GET    (/:locale)/general(.:format)                                                             forum#general {:locale=>/en|de/}
            ruby_on_rails GET    (/:locale)/ruby-on-rails(.:format)                                                       forum#rubyonrails {:locale=>/en|de/}
                off_topic GET    (/:locale)/off-topic(.:format)                                                           forum#offtopic {:locale=>/en|de/}
                   signup GET    (/:locale)/signup(.:format)                                                              users#new {:locale=>/en|de/}
                    login GET    (/:locale)/login(.:format)                                                               sessions#new {:locale=>/en|de/}
                          POST   (/:locale)/login(.:format)                                                               sessions#create {:locale=>/en|de/}
                   logout DELETE (/:locale)/logout(.:format)                                                              sessions#destroy {:locale=>/en|de/}
                    users GET    (/:locale)/users(/page/:page)(.:format)                                                  users#index {:locale=>/en|de/}
                          GET    (/:locale)/users(.:format)                                                               users#index {:locale=>/en|de/}
                          POST   (/:locale)/users(.:format)                                                               users#create {:locale=>/en|de/}
                 new_user GET    (/:locale)/users/new(.:format)                                                           users#new {:locale=>/en|de/}
                edit_user GET    (/:locale)/users/:id/edit(.:format)                                                      users#edit {:locale=>/en|de/}
                     user GET    (/:locale)/users/:id(.:format)                                                           users#show {:locale=>/en|de/}
                          PATCH  (/:locale)/users/:id(.:format)                                                           users#update {:locale=>/en|de/}
                          PUT    (/:locale)/users/:id(.:format)                                                           users#update {:locale=>/en|de/}
                          DELETE (/:locale)/users/:id(.:format)                                                           users#destroy {:locale=>/en|de/}
          password_resets POST   (/:locale)/password_resets(.:format)                                                     password_resets#create {:locale=>/en|de/}
       new_password_reset GET    (/:locale)/password_resets/new(.:format)                                                 password_resets#new {:locale=>/en|de/}
      edit_password_reset GET    (/:locale)/password_resets/:id/edit(.:format)                                            password_resets#edit {:locale=>/en|de/}
           password_reset PATCH  (/:locale)/password_resets/:id(.:format)                                                 password_resets#update {:locale=>/en|de/}
                          PUT    (/:locale)/password_resets/:id(.:format)                                                 password_resets#update {:locale=>/en|de/}
                          GET    (/:locale)/*category(.:format)                                                           posts#index {:category=>/(?x)gener...rials/, :locale=>/en|de/}
                    posts GET    (/:locale)/*category/posts(/page/:page)(.:format)                                        posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
                          GET    (/:locale)/*category/posts(.:format)                                                     posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
                          POST   (/:locale)/*category/posts(.:format)                                                     posts#create {:category=>/(?x)genera..torials/, :locale=>/en|de/}
                 new_post GET    (/:locale)/*category/posts/new(.:format)                                                 posts#new {:category=>/(?x)genera...torials/, :locale=>/en|de/}
                edit_post GET    (/:locale)/*category/posts/:id/edit(.:format)                                            posts#edit {:category=>/(?x)genera...orials/, :locale=>/en|de/}
                     post GET    (/:locale)/*category/posts/:id(.:format)                                                 posts#show {:category=>/(?x)genera...rials/, :locale=>/en|de/}
                          PATCH  (/:locale)/*category/posts/:id(.:format)                                                 posts#update {:category=>/(?x)gene...rials/, :locale=>/en|de/}
                          PUT    (/:locale)/*category/posts/:id(.:format)                                                 posts#update {:category=>/(?x)genera...orials/, :locale=>/en|de/}
                          DELETE (/:locale)/*category/posts/:id(.:format)                                                 posts#destroy {:category=>/(?x)gene...rials/, :locale=>/en|de/}
  edit_account_activation GET    /account_activations/:id/edit(.:format)                                                  account_activations#edit
       rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

1 个答案:

答案 0 :(得分:0)

也许尝试为form_for助手指定所有选项:

<%= form_for :post, url: post_path(@post, category: params[:category]), method: :patch do |f| %>