RoR:我为routes.rb添加了什么才能使用?

时间:2012-09-18 18:29:05

标签: ruby-on-rails-3 routing

我有一个用户可以制作微博的应用程序。但是在表单中输入一个并按帖子,它表示没有路由匹配[POST]“/ users / 1”。这是我当前的routes.rb文件。

SampleApp::Application.routes.draw do
  resources :users do
    resources :comments

    member do
      get :following, :followers
    end
  end
  resources :sessions, only: [:new, :create, :destroy]
  resources :microposts, only: [:create, :destroy] do
    resources :comments

  end
  resources :relationships, only: [:create, :destroy]

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'


  # The priority is based upon order of creation:

这是我运行rake路线时的输出:

user_comments GET    /users/:user_id/comments(.:format)                    comments#index
                       POST   /users/:user_id/comments(.:format)                    comments#create
      new_user_comment GET    /users/:user_id/comments/new(.:format)                comments#new
     edit_user_comment GET    /users/:user_id/comments/:id/edit(.:format)           comments#edit
          user_comment GET    /users/:user_id/comments/:id(.:format)                comments#show
                       PUT    /users/:user_id/comments/:id(.:format)                comments#update
                       DELETE /users/:user_id/comments/:id(.:format)                comments#destroy
        following_user GET    /users/:id/following(.:format)                        users#following
        followers_user GET    /users/:id/followers(.:format)                        users#followers
                 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
                       PUT    /users/:id(.:format)                                  users#update
                       DELETE /users/:id(.:format)                                  users#destroy
              sessions POST   /sessions(.:format)                                   sessions#create
           new_session GET    /sessions/new(.:format)                               sessions#new
               session DELETE /sessions/:id(.:format)                               sessions#destroy
    micropost_comments GET    /microposts/:micropost_id/comments(.:format)          comments#index
                       POST   /microposts/:micropost_id/comments(.:format)          comments#create
 new_micropost_comment GET    /microposts/:micropost_id/comments/new(.:format)      comments#new
edit_micropost_comment GET    /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit
     micropost_comment GET    /microposts/:micropost_id/comments/:id(.:format)      comments#show
                       PUT    /microposts/:micropost_id/comments/:id(.:format)      comments#update
                       DELETE /microposts/:micropost_id/comments/:id(.:format)      comments#destroy
            microposts POST   /microposts(.:format)                                 microposts#create
             micropost DELETE /microposts/:id(.:format)                             microposts#destroy
         relationships POST   /relationships(.:format)                              relationships#create
          relationship DELETE /relationships/:id(.:format)                          relationships#destroy
                  root        /                                                     static_pages#home
                signup        /signup(.:format)                                     users#new
                signin        /signin(.:format)                                     sessions#new
               signout DELETE /signout(.:format)                                    sessions#destroy
                  help        /help(.:format)                                       static_pages#help
                 about        /about(.:format)                                      static_pages#about
               contact        /contact(.:format)                                    static_pages#contact

我想我真正需要的是[POST] users /:id路由到微博#create,但我不知道它的语法。

P.S。如果我添加帖子“users /:id”,则:controller => “users / update”,我收到错误:

ArgumentError (missing :action):
  config/routes.rb:4:in `block (2 levels) in <top (required)>'
  config/routes.rb:2:in `block in <top (required)>'
  config/routes.rb:1:in `<top (required)>'

这是与控制器交谈的表格(我认为)

<%= form_for :micropost do |f| %>

  <div class="field no-indent">
    <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
    <%= hidden_field_tag 'micropost[kind]', "purchase" %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

1 个答案:

答案 0 :(得分:1)

在您的form_for中,您只需指定网址即可。我认为这应该有效。     &lt;%= form_for:micropost,:html =&gt; {:method =&gt; :post,:url =&gt; microposts_path} do | f | %GT;

  <div class="field no-indent">
    <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
    <%= hidden_field_tag 'micropost[kind]', "purchase" %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

另请告诉我什么是html输出。在form_for中:html参数作为hashmap给出,其中key是表单属性,值是属性值。

由于