Note that there are some explanatory texts on larger screens.

plurals
  1. POfields_for not working on update
    primarykey
    data
    text
    <p>I think this is a stupid mistake… When i create a record, my "resources" and "page_settings" tables getting populated.</p> <p>But my "page_setting" does nothing when i try to update the record.</p> <p>My models:</p> <pre><code>class Resource &lt; ActiveRecord::Base has_one :page_setting accepts_nested_attributes_for :page_setting end class PageSetting &lt; ActiveRecord::Base belongs_to :resource end </code></pre> <p>Here is the resources controller:</p> <pre><code>class ResourcesController &lt; ApplicationController # Initialize resource and belonging type model before_filter :build_resource_and_type, :only =&gt; [:new, :create] before_filter :get_resource_and_type, :only =&gt; [:edit, :update] def new end def create if @resource.save flash[:notice] = "Resource wurde erstellt" redirect_to root_url else flash[:error] = "Resource konnte nicht erstellt werden" render :action =&gt; 'new' end end def edit end def update if @resource.update_attributes(params[:resource]) flash[:notice] = "#{@type_name} #{@resource.title} wurde aktualisiert" redirect_to root_url else flash[:error] = "#{@type_name} #{@resource.title} konnte nicht aktualisiert werden" render :action =&gt; 'edit' end end private def build_resource_and_type # Get type from URL param (new action) or hidden field param (create action) type = params[:type_name] || params[:resource][:type_name] @resource = current_user.microsite.resources.new(params[:resource]) @resource.type_name = type # Build belonging model depending on type param case type when 'page' @resource.build_page_setting(params[:page_setting]) @type_name = 'page' end end def get_resource_and_type @resource = current_user.microsite.resources.find(params[:id]) @type_name = @resource.type_name end end </code></pre> <p>And the essential part of my resource form:</p> <pre><code>&lt;%= form_for @resource do |resource_form| %&gt; &lt;%= resource_form.hidden_field :type_name, :value =&gt; @type_name %&gt; … &lt;%= fields_for @resource.page_setting do |page_form| %&gt; &lt;%= page_form.label :content, "Text" %&gt; &lt;%= page_form.text_area :content %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload