Я получаю замечательную ошибку в проекте рельсов. У меня есть контроллер «article.rb». Я использую paperclip для прикрепления нескольких изображений, и для этого я использую модель «article_image». Все хорошо, когда я приступаю к новым действиям, чтобы создать новую статью и проверить новую запись. Я получаю следующее сообщение об ошибке:Скрепка: неопределенный метод `new_record? ' для nil: NilClass
undefined method `new_record?' for nil:NilClass
articles_controller.rb
class ArticlesController < ApplicationController
before_action :confirm_logged_in
def index
@articles= Article.paginate(page: params[:page],per_page:15).sorted
end
def new
@articles=Article.new()
3.times {@articles.article_images.build}
end
def create
@articles= Article.new(article_params)
if @articles.save
flash[:notice]="article created successfully"
redirect_to(:action =>"index")
else
render("new")
end
end
def show
@articles= Article.find(params[:id])
end
def edit
@articles= Article.find(params[:id])
4.times {@articles.article_images.build}
end
def update
@articles=Article.find(params[:id])
if @articles.update_attributes(article_params)
flash[:notice]="Article updated successfully"
redirect_to(:action=>"show",:id=>@articles.id)
else
render("edit")
end
end
def delete
@articles= Article.find(params[:id])
end
def destroy
@articles=Article.find(params[:id])
@articles.destroy
redirect_to(:action => 'index')
end
private
def article_params
params.require(:articles).permit(:title,:position,:visible,:body,:created_at, article_imgs_attributes: [:photo])
end
end
модели/article.rb
класс Статья < ActiveRecord :: Base
has_many :article_images, :dependent => :destroy
accepts_nested_attributes_for :article_images
belongs_to :admin_user
end
модели/article_image. rb
class ArticleImage < ActiveRecord::Base
belongs_to :article
has_attached_file :photo, :styles => {:medium => "300x300>", :thumb => "100x100#"}
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
end
вид/статьи/новые/html.erb
<div class="page-header"><h1>Articles</h1></div>
<%= link_to("back to article",{:action =>"index"}, :class =>"action index") %>
<%= form_for(:articles, :html =>{:multipart => true}, :url=>{:action =>'create'}) do |f| %>
<%= f.fields_for :article_imgs do |builder| %>
<% if builder.object.new_record? %>
<p>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
</p>
<% end %>
<% end %>
<% end %>
<div class="form-submit">
<%= submit_tag("create article") %>
</div>
<% end %>
</div>
получаю ошибку в
> <% if builder.object.new_record? %>
Я использую 'скрепку', '~> 4.2.1' в этом проекте. Любой может помочь мне найти проблему?
Одним из результатов от вызова 'fields_for' возвращает nil, когда вызывается 'object'. Вот откуда исходит ваша ошибка - у nil нет метода 'new_record? '. – fzzfzzfzz
Какова цель этой строки '<% if builder.object.new_record? %> ', чего вы пытаетесь достичь? – Pavan