嵌套属性 - Cocoon Gem - form_for “表单中的第一个参数不能包含 nil 或为空” - Rails 5

Nested Attributes - Cocoon Gem - form_for “First argument in form cannot contain nil or be empty” - Rails 5

这是一个老歌,但是很老歌,所以我真的很抱歉问这个问题,但是在检查了 10 多个有类似错误的帖子之后,我仍然没有运气。

尝试使用 "form for" 使嵌套表单在 Cocoon gem 的帮助下工作。

尽管我遇到了 "First argument in form cannot contain nil or be empty" 错误。

这里是 _text_form partial.html.erb :

<%= form_for(@text, html: { multipart: true }) do |f| %>
 <%= render 'shared/error_messages', object: f.object %>
 <div class="field">
  <%= f.text_field :title, placeholder: "Enter Title" %>
  <%= f.text_field :publication, placeholder: "Enter Publication" %>
  <%= f.text_field :author, placeholder: "Enter Author" %>
  <%= f.text_field :url, placeholder: "Enter Url" %> 
 </div>
 <div>
  <h3>Quotes:</h3>
  <div id="quotes">
    <%= f.fields_for :quotes do |quote| %>
      <%= render 'shared/quote_fields', f: quote %>
    <% end %>

   <div class="links">
     <%= link_to_add_association 'add quote', f, :quotes, class: "btn btn-default" %>
   </div>
  </div>
 </div>


 <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

这是_quotes_fields.html.erb部分:

<div class="nested-fields">
 <%= f.text_area :content, placeholder: "Compose new quote..." %>
 <span class="picture">
  <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
 </span>
 <%= link_to_remove_association "remove quote", f, class: "btn btn-default" %>
</div>

<script type="text/javascript">
  $('#quote_picture').bind('change', function() {
   var size_in_megabytes = this.files[0].size/1024/1024;
   if (size_in_megabytes > 2) {
     alert('Maximum file size is 2MB. Please choose a smaller file.');
   }
  });
</script>

这是texts_controller.rb:

class TextsController < ApplicationController

 before_action :logged_in_user, only: [:create, :edit, :update, :destroy]
 before_action :correct_user, only: :destroy
 before_action :find_text, only: [:show, :edit, :update, :destroy]



 def show
 end

 def new
  @text = current_user.texts.build
 end

 def create
  @text = current_user.texts.build(text_params)
  if @text.save
   flash[:success] = "Text created!"
   redirect_to root_url
  else
   render 'static_pages/home'
  end
 end


 def edit
 end

 def update
  if @text.update(text_params)
   redirect_to @text
  else
  render 'edit'
  end
 end

 def destroy
  @text.destroy
  flash[:success] = "Text deleted"
  redirect_to request.referrer || root_url
 end

private

 def text_params
  params.require(:text).permit(:url, :title, :coverimage,
                              :publication, :author, :summary, quotes_attributes: [:id, :content, :picture, :_destroy])
 end

 def find_text
    @text = Text.find(params[:id])
 end

 def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
 end

end

首先抱歉,如果有任何简单而可怕的新手错误,非常感谢您的帮助。

更新: _text_form 部分在 ../static_pages/home.html.erb

中呈现

更新二: 这是 routes.rb:

Rails.application.routes.draw do

 get 'password_resets/new'

 get 'password_resets/edit'

 get 'sessions/new'

 root           'static_pages#home'
 get 'help'  => 'static_pages#help'
 get 'about' => 'static_pages#about'
 get 'signup' => 'users#new'
 get 'login' => 'sessions#new'
 post 'login' => 'sessions#create'
 delete 'logout' => 'sessions#destroy'
 resources :users do
  member do
   get :following, :followers
  end
 end
 resources :account_activations, only: [:edit]
 resources :password_resets, only: [:new, :create, :edit, :update]
 resources :quotes, only: [:create, :destroy]
 resources :texts, only: [:new, :create, :edit, :update]
 resources :relationships, only: [:create, :destroy]
 end

发生这个错误是因为@text 还没有定义,所以你只需要在你将渲染你的部分的动作中定义它,在这种情况下是你的静态页面控制器中的主页动作。