这是一个老人但很贵,因此我很抱歉,但在检查了10个以上类似错误的帖子后,到目前为止我仍然没有运气。
尝试使用“form for”来获取嵌套表单,以便在Cocoon gem的帮助下工作。
虽然我得到“形式中的第一个参数不能包含nil或为空”错误。
以下是 _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 partial :
<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
中呈现更新2: 这是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
答案 0 :(得分:1)
发生此错误是因为@text尚未定义,因此您只需要在操作中定义它,您将渲染部分内容,在这种情况下,您将在静态页面控制器中执行主页操作。