0

当我访问用户的个人资料页面时,我得到了这个。所以 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>

我也不明白如何将空对象传递给表单?表单的全部意义不在于创建对象吗?

4

1 回答 1

0

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

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

于 2012-09-14T23:08:24.003 回答