Question

i've a rails 4 instance running with a pages model which has different types of page contents like PageContent::Text which inherits from PageContent.

in my form i render all my @page.page_contents as you can see in the form snippet..

if i update my page the records doesnt get updated they are created as new records.

# update controller
class Admin::PagesController < Admin::BaseController

  def update
    @page = Page.find_by_url(params[:id])

    respond_to do |format|
      if @page.update_attributes(page_params(@page.type.parameterize.gsub('page','')))
        format.html { redirect_to edit_admin_page_path(@page) }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def page_params(type)
    params.require("#{type}_page".to_sym).permit(:name, :contents_attributes => [:body, :type])
  end

end

# content model
class PageContent::Text < PageContent
end

# page model
class Page < ActiveRecord::Base
  has_many :contents, class_name: 'PageContent', dependent: :destroy
  accepts_nested_attributes_for :contents
end

# form snippet
<textarea class="form-control wysihtml5" id="content_page_contents_attributes_0_body" name="content_page[contents_attributes][0][body]">
testsetseet</textarea>
<input id="content_page_contents_attributes_0_type" name="content_page[contents_attributes][0][type]" type="hidden" value="PageContent::Text" />
<input id="content_page_contents_attributes_0_id" name="content_page[contents_attributes][0][id]" type="hidden" value="1" />

any ideas would be very welcome!

thanks in advance

Was it helpful?

Solution

Maybe just adding :id to .permit(:name, :contents_attributes => [:body, :type])will solve the problem?

The result should look like :contents_attributes => [:body, :type, :id].

Also have a look on server stdout - it outputs which attributes are not permitted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top