0

我有一个应用程序,用户可以在其中发布微博。但是在表格中输入一个并按帖子,它说没有路线匹配[POST]“/users/1”。我不明白这一点。听起来它正在尝试创建 /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 路由到 microposts#create,但我不知道它的语法。

ps 如果我添加帖子 "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 %>

另外,这里是 microposts_controller.rb

class MicropostsController < ApplicationController

  before_filter :correct_user,   only: :destroy

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def new
    @micropost = Micropost.new(params[:micropost])
  end

  def show

    @micropost = Micropost.find(params[:id])
    @microposts = Micropost.where(:user_id => @user.id)
    @purchases = @microposts.where(:kind => "purchase")
    @sales = @microposts.where(:kind => "sale")


  end

和 users_controller.rb

class UsersController < ApplicationController
  before_filter :signed_in_user, 
                only: [:index, :edit, :update, :destroy, :following, :followers]
  before_filter :correct_user,   only: [:edit, :update]
  before_filter :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end
4

1 回答 1

0

根据评论,您的问题是多方面的:

1)恢复form_for到以前的样子

2)@micropost在呈现该表单的相应操作中设置变量。在 users#show 的情况下,这将是UsersController

def show
  @micropost = Micropost.new
end
于 2012-09-19T05:07:59.770 回答