2012-07-12 96 views
0

我用Rails 3.2.6Rails的嵌套形式插入空

我們有在運作,直到最近(不完全肯定時錯誤發生開始)嵌套形式。現在查詢傳入空值。

輸出示例:

Started POST "/articles" for 127.0.0.1 at 2012-07-12 11:04:16 -0600 
Processing by ArticlesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"thing=", "article"=>{"asset_attributes"=>{"name"=>"asdf", "short_desc"=>"asdfasdfsadg", "publish_date"=>"2012-07-12 11:4", "content_section_attributes"=>{"section_id"=>"1", "primary_section"=>"1"}, "section_ids"=>[""], "author_id"=>"", "display_authorbiography"=>"1", "last_modified_by"=>"1", "by_line"=>"", "content_image_attributes"=>{"image_id"=>""}, "keywords_string"=>"", "public_keywords_string"=>"", "meta_public_keywords"=>"", "meta_page_title"=>"", "meta_description"=>"", "meta_url_name"=>"", "guid"=>"", "canonical"=>"", "partner_id"=>"", "tagline"=>"", "series_id"=>"", "display_adsense"=>"1", "sweepstakes"=>""}, "content"=>"<p>asdfasgasg</p>"}, "img_size"=>"i01", "show_cap"=>"1", "img_pos"=>"left", "commit"=>"Create"} 
    User Load (0.2ms) SELECT `people`.* FROM `people` WHERE `people`.`type` IN ('User') AND `people`.`id` = 1 LIMIT 1 /*application:TmnCoreCms,controller:articles,action:create*/ 
    Section Load (0.1ms) SELECT `sections`.* FROM `sections` /*application:TmnCoreCms,controller:articles,action:create*/ 
    Site Load (0.1ms) SELECT `sites`.* FROM `sites` WHERE `sites`.`id` IN (1) /*application:TmnCoreCms,controller:articles,action:create*/ 
    Site Load (0.1ms) SELECT `sites`.* FROM `sites` /*application:TmnCoreCms,controller:articles,action:create*/ 
    Section Load (0.1ms) SELECT `sections`.* FROM `sections` WHERE `sections`.`site_id` IN (1) /*application:TmnCoreCms,controller:articles,action:create*/ 
    Role Load (0.1ms) SELECT `roles`.* FROM `roles` INNER JOIN `user_roles` ON `roles`.`id` = `user_roles`.`role_id` WHERE `user_roles`.`user_id` = 1 /*application:TmnCoreCms,controller:articles,action:create*/ 
    (0.1ms) BEGIN /*application:TmnCoreCms,controller:articles,action:create*/ 
    (0.1ms) COMMIT /*application:TmnCoreCms,controller:articles,action:create*/ 
    (0.1ms) BEGIN /*application:TmnCoreCms,controller:articles,action:create*/ 
    (0.1ms) COMMIT /*application:TmnCoreCms,controller:articles,action:create*/ 
    (0.1ms) BEGIN /*application:TmnCoreCms,controller:articles,action:create*/ 
    SQL (0.2ms) INSERT INTO `articles` (`content`, `created_at`, `static_url`, `updated_at`) VALUES ('<p>asdfasgasg</p>', '2012-07-12 17:04:16', NULL, '2012-07-12 17:04:16') /*application:TmnCoreCms,controller:articles,action:create*/ 
    Article Load (0.3ms) SELECT `articles`.* FROM `articles` WHERE `articles`.`id` = 25 LIMIT 1 /*application:TmnCoreCms,controller:articles,action:create*/ 
{} 
    SQL (0.0ms) INSERT INTO `assets` (`aasm_state`, `author_id`, `by_line`, `canonical`, `content_id`, `content_type`, `created_at`, `creator_id`, `display_adsense`, `display_authorbiography`, `guid`, `last_modified_by`, `legacyid`, `live_date`, `meta_description`, `meta_page_title`, `meta_public_keywords`, `meta_url_name`, `migration_guid`, `name`, `partner_id`, `publish_date`, `publish_version`, `series_id`, `short_desc`, `sweepstakes`, `tagline`, `updated_at`, `url_name`) VALUES ('new', NULL, NULL, NULL, 25, 'Article', '2012-07-12 17:04:16', NULL, 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2012-07-12 17:04:16', NULL) /*application:TmnCoreCms,controller:articles,action:create*/ 
    SQL (0.2ms) INSERT INTO `content_sections` (`content_id`, `content_type`, `created_at`, `primary_section`, `section_id`, `updated_at`) VALUES (53, 'Asset', '2012-07-12 17:04:16', 1, NULL, '2012-07-12 17:04:16') /*application:TmnCoreCms,controller:articles,action:create*/ 
    (33.6ms) COMMIT /*application:TmnCoreCms,controller:articles,action:create*/ 
Redirected to http://localhost:3000/articles/53- 
Completed 302 Found in 63ms (ActiveRecord: 35.3ms) 

所以你可以看到屬性被髮送。我們的控制器是這樣的。

def new 
    @article = Article.new 
    @asset = @article.build_asset 
    end 

    def create 
    @article = Article.new(params[:article]) 
    if @article.save 
     flash[:notice] = 'Article was successfully created.' 
     redirect_to @article 
    else 
     render :action => "new" 
    end 
    end 

相關的模型信息:

class ContentType < ActiveRecord::Base 
    self.abstract_class = true 
    validates_presence_of :asset, :message => "Must have an asset" 
    has_one :asset, :as => :content, :dependent => :destroy 
    delegate :name, :url_name, :author, :creator, :publish_date, :short_desc, 
      :aasm_state, :live_date, :keywords, 
      :to => :asset, :allow_nil => true 
    delegate :keywords_string, :public_keywords_string, 
      :to => :asset, 
      :allow_nil => true 

    def initialize(*args) 
    super(*args) 
    end 


    def to_param 
    "#{asset.id}-#{url_name}" 
    end 
end 

class Asset < ActiveRecord::Base 
    include AASM 
    include Keywordable 
    attr_accessible :author_id, 
        :by_line, 
        :canonical, 
        :content_image_attributes, 
        :content_section_attributes, 
        :creator_id, 
        :display_adsense, 
        :display_authorbiography, 
        :guid, 
        :keywords, 
        :keywords_string, 
        :last_modified_by, 
        :meta_description, 
        :meta_page_title, 
        :meta_public_keywords, 
        :meta_url_name, 
        :name, 
        :partner_id, 
        :public_keywords_string, 
        :publish_date, 
        :related_content_attributes, 
        :section_ids, 
        :series_id, 
        :short_desc, 
        :sweepstakes, 
        :syndication_partner_ids, 
        :tagline, 
        :url_name 

    belongs_to :author, :class_name => 'Person' 
    belongs_to :content, :polymorphic => true 
    has_many :content_sections, :as => :content, :conditions => {:primary_section => nil}, :dependent => :destroy 

class Article < ContentType 
    attr_accessible :images_attributes, 
        :content_images_attributes, 
        :content_text, 
        :content, 
        :asset_attributes, 
        :content_attributes 

我真的不喜歡這個協會成立,但它是它是什麼。發生了什麼事是嵌套形式

<%= f.fields_for :asset do |asset| %> 
    <fieldset class="span8"> 
     <legend>Asset Description</legend> 
     <div id='a_title'> 
      <%= asset.label :name, "Title <span class='req'>*</span>".html_safe %> 
      <%= asset.text_field :name, :class => 'asset_textarea counter', "data-max" => 255 %> 
      <br/><span></span> 
     </div> 
     <div id='a_desc'> 
      <%= asset.label :short_desc, "Teaser<span class='req'>*</span>".html_safe %> 
        <%= asset.text_area :short_desc, :class => "span7 asset_textarea counter", "data-max" => 255, :rows => 7 %> 
        <br/><span></span> 
     </div> 
    </fieldset> 
<% end %> 

以某種方式傳遞字段和Rails是把在零點。我沒有attr_accessible警告。如果我對文章進行更新,它會保存資產數據。因此,我認爲初始化出了問題,但我不確定是什麼。

TLDR 儘管attr_accessible具有嵌套屬性,Rails並沒有將值傳遞到SQL查詢中。

編輯

的contentTypesController有一些在此之前,我原本以爲是問題的過濾器(他們仍然可我還是搞清楚是如何被使用的話)

class ContentTypesController < ApplicationController 

    before_filter :clean_syndication_partners, :only => [:update] 
    before_filter :setup_existing_content_instance_variable, :except => [:index, :new, :create, :sort, :update_json, :vid_search, :search_library] 
    #before_filter :setup_new_content_instance_variable, :only => [:new, :create] 
    before_filter :get_sections 
    before_filter :get_sites 
    def clean_syndication_partners 
    @asset = Asset.find(params[:id], :include => :content) 
    @asset.syndication_partners = [] 
    end 

    def get_sites 
    @sites_all = Site.all(:include => :sections) 
    end 
    def get_sections 
    @sections = Section.all(:include => :site) 
    end 

    def setup_existing_content_instance_variable 
    @asset = Asset.find(params[:id], :include => :content) if @asset.nil? 
    instance_variable_set("@#{@asset.content.class.name.underscore}", @asset.content) 
    end 

    # def setup_new_content_instance_variable 
    # pub_keyword = false 
    # current_class = self.controller_name.singularize 
    # @content_type = current_class.downcase 
    # if params[current_class] 
    #  cur_content= current_class.camelize.constantize.new(params[current_class]) 
    #  #cur_content.asset = Asset.update_attributes(params[current_class][:asset_attributes]) 
    # else 
    #  cur_content= current_class.camelize.constantize.new 
    #  #cur_content.asset=Asset.new 
    # end 
    # #cur_content.asset.creator=current_user 
    # #instance_variable_set(:@asset, cur_content.asset) 
    # instance_variable_set("@#{current_class}", cur_content) 
    # end 

end 
+0

如果有人能夠指引我走向正確的方向,我會非常高興。我對發生錯誤的地方感到不知所措。 – Micharch54 2012-07-12 17:28:05

+0

任何過濾器之前? – Renra 2012-07-12 17:37:08

+0

我已編輯帖子以顯示過濾器。這也是我的第一個想法。 – Micharch54 2012-07-12 18:07:09

回答

0

該問題最終導致從3.2.5到3.2.6。我認爲這是因爲我們將類繼承用於共享資產關係的唯一目的。我們將其回滾,我們將在未來重新審視這種關係。

0

你忘了將其添加到您的文章模型中:

accepts_nested_attributes_for :asset, allow_destroy: true 
+0

我其實已經嘗試過,但看起來我們的協會很奇怪。它給我一個路由錯誤(我知道它來自最初的開發者決定使用:資產而不是:資產)。我會看看是否可以輕鬆修復。謝謝。 – Micharch54 2012-07-12 18:08:24