Rails 4 "undefined method `image_changed?' for #" 使用载波编辑
Rails 4 "undefined method `image_changed?' for #" on edit with carrierwave
我有这个 rails 应用程序,其中 "Diys" 已分配给它们 "Steps",那些 "Steps" 已分配给它们的图像("add_images_to_steps")承运人。
创建新的 "Diys" 工作正常,也显示它们,但是当我尝试编辑它们时,我得到
undefined method `image_changed?' for #Diy:0xb8a1d20
提交表单后,即使我没有在表单中包含 "add_images_to_steps" 个字段。
diys_controller.rb
def edit
@diy = Diy.find(params[:id])
@diy.steps.all
@diy.add_images_to_steps.all
end
...
def update
respond_to do |format|
if @diy.update(diy_params)
format.html { redirect_to @diy, notice: 'Diy was successfully updated.' }
format.json { render :show, status: :ok, location: @diy }
else
format.html { render :edit }
format.json { render json: @diy.errors, status: :unprocessable_entity }
end
end
end
...
private
def diy_params
params.require(:diy).permit(:title, :summary, :tip, :warning, steps_attributes: [:step_content, add_images_to_steps_attributes: [:image]])
end
diys/edit.html.erb
<h1>Editing Diy</h1>
<%= form_for(@diy, :html => {:multipart => true}) do |f| %>
<% if @diy.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@diy.errors.count, "error") %> prohibited this diy from being saved:</h2>
<ul>
<% @diy.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="form_div">
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<%= f.label :summary %><br>
<%= f.text_area :summary %><br>
<%= f.label "Tips" %><br>
<%= f.text_area :tip %><br>
<%= f.label "Warnings" %><br>
<%= f.text_area :warning %><br>
<%= f.fields_for :steps do |step_fields| %>
<%= step_fields.text_area :step_content %><br>
<% end %>
<%= f.submit %>
<% end %>
<%= link_to 'Show', @diy %> |
<%= link_to 'Back', diys_path %>
models/diy.rb
class Diy < ActiveRecord::Base
has_and_belongs_to_many :vehicles
has_many :steps, dependent: :destroy
accepts_nested_attributes_for :steps, reject_if: :all_blank
has_many :add_images_to_steps, :through => :steps, dependent: :destroy
accepts_nested_attributes_for :add_images_to_steps, reject_if: :all_blank
mount_uploader :image, ImageUploader
searchable do
text :title, :default_boost => 2
text :summary
text :tip, :default_boost => 0.5
text :warning, :default_boost => 0.4
text :steps do
steps.map(&:step_content)
end
end
end
models/step.rb
class Step < ActiveRecord::Base
belongs_to :diy
has_many :add_images_to_steps, dependent: :destroy
accepts_nested_attributes_for :add_images_to_steps, reject_if: :all_blank
mount_uploader :image, ImageUploader
end
models/add_images_to_step.rb
class AddImagesToStep < ActiveRecord::Base
belongs_to :step
mount_uploader :image, ImageUploader
end
哦,多么愚蠢的错误。
mount_uploader :image, ImageUploader
diys 和 steps 模型中的行无用并导致此错误。
还必须为更新操作创建新参数以正确更改图像,使用旧参数它只是添加新图像而不删除旧图像
def diy_params_update
params.require(:diy).permit(:id, :title, :summary, :tip, :warning, steps_attributes: [:diy_id, :id, :step_content, add_images_to_steps_attributes: [:step_id, :id, :image]])
end
我有这个 rails 应用程序,其中 "Diys" 已分配给它们 "Steps",那些 "Steps" 已分配给它们的图像("add_images_to_steps")承运人。 创建新的 "Diys" 工作正常,也显示它们,但是当我尝试编辑它们时,我得到
undefined method `image_changed?' for #Diy:0xb8a1d20
提交表单后,即使我没有在表单中包含 "add_images_to_steps" 个字段。
diys_controller.rb
def edit
@diy = Diy.find(params[:id])
@diy.steps.all
@diy.add_images_to_steps.all
end
...
def update
respond_to do |format|
if @diy.update(diy_params)
format.html { redirect_to @diy, notice: 'Diy was successfully updated.' }
format.json { render :show, status: :ok, location: @diy }
else
format.html { render :edit }
format.json { render json: @diy.errors, status: :unprocessable_entity }
end
end
end
...
private
def diy_params
params.require(:diy).permit(:title, :summary, :tip, :warning, steps_attributes: [:step_content, add_images_to_steps_attributes: [:image]])
end
diys/edit.html.erb
<h1>Editing Diy</h1>
<%= form_for(@diy, :html => {:multipart => true}) do |f| %>
<% if @diy.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@diy.errors.count, "error") %> prohibited this diy from being saved:</h2>
<ul>
<% @diy.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="form_div">
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<%= f.label :summary %><br>
<%= f.text_area :summary %><br>
<%= f.label "Tips" %><br>
<%= f.text_area :tip %><br>
<%= f.label "Warnings" %><br>
<%= f.text_area :warning %><br>
<%= f.fields_for :steps do |step_fields| %>
<%= step_fields.text_area :step_content %><br>
<% end %>
<%= f.submit %>
<% end %>
<%= link_to 'Show', @diy %> |
<%= link_to 'Back', diys_path %>
models/diy.rb
class Diy < ActiveRecord::Base
has_and_belongs_to_many :vehicles
has_many :steps, dependent: :destroy
accepts_nested_attributes_for :steps, reject_if: :all_blank
has_many :add_images_to_steps, :through => :steps, dependent: :destroy
accepts_nested_attributes_for :add_images_to_steps, reject_if: :all_blank
mount_uploader :image, ImageUploader
searchable do
text :title, :default_boost => 2
text :summary
text :tip, :default_boost => 0.5
text :warning, :default_boost => 0.4
text :steps do
steps.map(&:step_content)
end
end
end
models/step.rb
class Step < ActiveRecord::Base
belongs_to :diy
has_many :add_images_to_steps, dependent: :destroy
accepts_nested_attributes_for :add_images_to_steps, reject_if: :all_blank
mount_uploader :image, ImageUploader
end
models/add_images_to_step.rb
class AddImagesToStep < ActiveRecord::Base
belongs_to :step
mount_uploader :image, ImageUploader
end
哦,多么愚蠢的错误。
mount_uploader :image, ImageUploader
diys 和 steps 模型中的行无用并导致此错误。
还必须为更新操作创建新参数以正确更改图像,使用旧参数它只是添加新图像而不删除旧图像
def diy_params_update
params.require(:diy).permit(:id, :title, :summary, :tip, :warning, steps_attributes: [:diy_id, :id, :step_content, add_images_to_steps_attributes: [:step_id, :id, :image]])
end