2016-02-07 1 views
0

У меня есть это приложение для рельсов с« Diys », которому назначены« Шаги », у этих« Шагов »есть изображения (« add_images_to_steps »), назначенные им carriervawe. Создание нового «DIYs» работает отлично, и показывая их тоже, но когда я пытаюсь редактировать их я получитьRails 4 "undefined method` image_changed? ' для # "при редактировании с помощью несущей волны

неопределенный метод `image_changed? для #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 %> 

модели/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 

модели/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 

модели/add_images_to_step.rb

class AddImagesToStep < ActiveRecord::Base 
    belongs_to :step 
    mount_uploader :image, ImageUploader 
end 
+0

при редактировании изображений добавить требовать Params ':' id' как add_images_to_steps_attributes: [: id,: image] ' –

+0

Благодарим за помощь! Это не решило мою проблему, вызванную избыточными строками «mount_uploader ...» в шагах и модели diys. Но помог с следующей проблемой, с которой я столкнулся :) –

ответ

2

Ах, как глупая ошибка.

mount_uploader: изображение, ImageUploader

линия в DIYs и шаги модель была бесполезна и вызвала эту ошибку.

также сделать новый Params для действий обновления, чтобы изменить изображения должным образом, со старой Params он просто добавил новое изображение без удаления старого

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 
Смежные вопросы