为什么我为NilClass:Class获取未定义的方法`model_name'?

时间:2012-09-14 22:54:49

标签: ruby-on-rails-3

当我访问用户的个人资料页面时,我得到了这个。所以localhost:3000 / users / 1例如......

我在第1行得到它

1: <%= form_for(@micropost) do |f| %>
2:   <%= render 'shared/error_messages', object: f.object %>
3:   <div class="field no-indent">
4:     <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>

所以我有两个微博表格,一个在这里

<%= form_for(@micropost, :html => { :id => "sale" }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field no-indent">
    <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
    <%= hidden_field_tag 'micropost[kind]', "sale" %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

另一个在这里

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <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 %>

这是我的路线。

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
    resources :kind
  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'

以下是页面本身的代码(用户显示)

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

      <section>
        <div id= "purchases">
          <%= render 'shared/micropost_form_purchase' %>
        </div>
        <div id="sales">
          <%= render 'shared/micropost_form_sale' %>
        </div>
      </section>


<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
  <div class="span8">

    <% if @user.microposts.any? %>
      <h3>Purchases I am interested in (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>

我也不明白你如何将空对象传递给表单?创建对象的形式不是全部吗?

1 个答案:

答案 0 :(得分:0)

根据我今天偶然发现的文章,你传递给form_for的对象(在这种情况下是@micropost)可能是零或空的。

这是上面提到的文章:http://schneems.com/post/31460949407/raise-hell-better-programming-through-error-messages