我在 Rails 3.2.6
我们有一个嵌套表单直到最近才起作用(不完全确定错误何时开始发生)。现在查询传入空值。
示例输出:
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 警告。如果我对文章进行更新,它将保存资产数据。所以我认为初始化出现了问题,但我不确定是什么。
尽管 attr_accessible 具有嵌套属性,但TLDR 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