0

我正在关注http://ruby.railstutorial.org/ ,我在第 10 章——构建微帖子,但我将 the_microposts 命名为微帖子,然后我遇到了这个问题:

当我访问 static_page/home 时,它​​显示:

#<#:0xb628402c> 的未定义方法“the_microposts_path”

应用程序跟踪是:

app/views/shared/_the_micropost_form.html.erb:1:in _app_views_shared__the_micropost_form_html_erb___1052260201__619399208' app/views/static_pages/home.html.erb:9:in_app_views_static_pages_home_html_erb_ 733532872 _628014758'

好像我没有定义方法?但我已经在我的 static_pages 控制器中定义了它:

def home
    @the_micropost = current_user.the_microposts.build if signed_in?
end

我的主页:

<section>
        <%= render 'shared/the_micropost_form' %>
</section>

我的 _the_micropost_form.html.erb 是:

<%= form_for(@the_micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new micropost..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

我的用户模型:

has_many :the_microposts, dependent: :destroy

the_micropost 模型:

belongs_to :user

我的路线:

resources :users
resources :sessions, only: [:new, :create, :destroy]
root  'static_pages#home'
match '/signup',   to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help',    to: 'static_pages#help', via: 'get'
match '/about',   to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'

有什么问题吗??我总是按照步骤进行,是不是因为我定义了不同的名称?(the_microposts 为 microposts)

4

1 回答 1

0

您需要为Micropost资源定义路由,就像您为usersand定义的一样sessions。更新你config/routes的包括resource :microposts.

resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts
root  'static_pages#home'
...
于 2013-08-18T06:19:48.730 回答