rails:嵌套表单不在视图中呈现

时间:2016-01-25 01:19:05

标签: ruby-on-rails ruby ruby-on-rails-4

我有这样的模特

post.rb

has_many :items
accepts_nested_attributes_for :items

item.rb的

has_one :heading, dependent: :destroy
has_one :content, dependent: :destroy
has_one :link, dependent: :destroy
has_one :movie, dependent: :destroy
has_one :photo, dependent: :destroy
has_one :quate, dependent: :destroy

accepts_nested_attributes_for :heading
accepts_nested_attributes_for :content
accepts_nested_attributes_for :link
accepts_nested_attributes_for :movie
accepts_nested_attributes_for :photo
accepts_nested_attributes_for :quate

航向,内容,链接,电影,照片,quate.rb

belongs_to :item

posts_controller.rb

def new
        @post = current_user.posts.build
end

def create

    @post = current_user.posts.build(post_params)

    respond_to do |format|

        if @post.save
            format.html { redirect_to @post, notice: 'created!!' }
        else
            format.html { render :new }
        end
end

def post_params
        params.require(:post).permit(:title, :description, :image,:user_id,
        items_attributes:[:id, :order,
            heading_attributes:[:id, :head],
            photo_attributes:[:id, :image, :title, :q_url],
            movie_attributes:[:id, :y_url],
            quate_attributes:[:id, :quate, :q_url, :q_title, :q_comment],
            content_attributes:[:id, :content],
            link_attributes:[:id, :url, :l_text],
            twitter_attributes:[:id, :t_url]
            ])
end

文章/ new_and_edit.html.erb

<%= form_for(@post) do |f| %>
    <% if @post.errors.any? %>
      <ul>
        <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
        <% end %>
      </ul>
    <% end %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :description%>
<%= f.text_field :description %>

<%= f.label :image %>
<%= f.file_field :image %>

<%= f.hidden_field :user_id %>

<%= f.submit %>

  <%= render 'posts/item_form_fields', name: 'headings' %>
  <%= render 'posts/item_form_fields', name: 'contents' %>
  <%= render 'posts/item_form_fields', name: 'movies' %>
  <%= render 'posts/item_form_fields', name: 'quates' %>
  <%= render 'posts/item_form_fields', name: 'links' %>
  <%= render 'posts/item_form_fields', name: 'photos' %>

文章/ _item_form_fields.html.erb

<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>

items / _form_fields.html.erb

<%= m.fields_for :items, @item do |b| %>
  <%= b.hidden_field :order, value: '0' %>

  <%= render "#{name}/form_fields", b: b %>
<% end %>

标题/ _form_fields.html.erb

<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

&#34;我有一个&#34; _form_fields.html.erb&#34;内容,链接,电影,照片和状态。 &#34;

当我运行rails服务器时,嵌套模型表单不会显示在视图上。 我也按了#34;提交&#34;,控制台说

  

param丢失或值为空:post

我不明白为什么会发生这种情况。 你有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

你需要纠正一些重要的事情

posts#new行动中

def new 
  @post = current_user.posts.build
  @item = @post.items.build
  @item.build_heading
  @item.build_content
  @item.build_link
  @item.build_movie
  @item.build_photo
  @item.build_quate
end

所有更改后的表单代码应该如下所示

<%= form_for(@post) do |f| %>
    <% if @post.errors.any? %>
      <ul>
        <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
        <% end %>
      </ul>
    <% end %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :description%>
<%= f.text_field :description %>

<%= f.label :image %>
<%= f.file_field :image %>

<%= f.hidden_field :user_id %>
<%= f.fields_for @item do |m| %>
  <%= render 'items/form_fields', m: m %>
  <%= render 'headings/form_fields', m: m %>
  <%= render 'contents/form_fields', m: m %>
  <%= render 'movies/form_fields', m: m %>
  <%= render 'quates/form_fields', m: m %>
  <%= render 'links/form_fields', m: m %>
  <%= render 'potos/form_fields', m: m %>
<% end %>
<%= f.submit %>
<% end %>

您需要更改部分内容,如下所示

#items/_form_fields.html.erb

  <%= m.hidden_field :order, value: '0' %>

#headings/_form_fields.html.erb

<%= m.fields_for :heading do |h| %>
  <%= h.text_field :head %>
<% end %>

其他部分也是如此。