0

ユーザーがマイクロポストを作成できるアプリケーションがあります。ただし、フォームに 1 つ入力して投稿を押すと、[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 routes を実行したときの出力です。

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 を microposts#create にルーティングすることだと思いますが、その構文はわかりません。

ps post post "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 %>
4

1 に答える 1

1

form_for では、url を指定するだけです。これはうまくいくはずだと思います。<%= form_for :micropost, :html => {:method => :post, :url => microposts_path} 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 %>

また、html出力とは何か教えてください。form_for では、:html パラメータはハッシュマップとして与えられます。ここで、キーはフォーム属性であり、値は属性値です。

ありがとう

于 2012-09-18T18:43:44.503 に答える