Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails:嵌套形式不';看不见_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails rails:嵌套形式不';看不见

Ruby on rails rails:嵌套形式不';看不见,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有这样的模型 <%= form_for(@post) do |f| %> <% if @post.errors.any? %> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul>

我有这样的模型

<%= 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 %>
post.rb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>
item.rb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>
标题、内容、链接、电影、照片、季度rb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

posts\u controller.rb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

posts/new_和_edit.html.erb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

posts/\u item\u form\u fields.html.erb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

items/\u form\u fields.html.erb

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

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

headers/\u form\u fields.html.erb

has_many :items
accepts_nested_attributes_for :items
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
belongs_to :item
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
<%= 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' %>
<%= form_for(@post, remote: false) do |m| %>
    <%= render 'items/form_fields', m: m, name: name %>
    <%= m.submit "submit" %>
<% end %>
<%= b.fields_for :heading, @item.build_heading do |h| %>
  <%= h.text_field :head %>
<% end %>

我有一个“\u form\u fields.html.erb”用于内容、链接、电影、照片和季度


当我运行rails服务器时,嵌套模型表单不会显示在视图中。 我还按了“提交”,控制台说

缺少参数或值为空:post

我不明白为什么会这样。 你知道吗


谢谢

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

在您的
帖子中#新建
操作

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 %>
#items/_form_fields.html.erb
#标题/_form_fields.html.erb

其余部分也一样。

谢谢!我可以看到项目的表单,以及项目的子表单。但是,当我单击提交时,似乎没有创建项目和项目模型子项的post数据。我是否仍需要更改posts控制器上的创建方法?@vanhiro您可以发布日志中生成的参数吗?