帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
我正在使用nested_attributes并试图通过Ryan Bates关于嵌套模型(#196)的截屏实现添加/删除字段,
这样做,它不会工作,但是当删除“link_to_add_fields”行时,它工作正常。
问题是我不知道我是否正确地完成了所有的关联。
<%= form_for(@item) do |f| %> <% if @item.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2> <ul> <% @item.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :item_type_id %><br /> <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %> <br /> <%= f.fields_for :item_parts do |parts_form| %> <%= render "item_part_fields", :p => parts_form %> <% end %> </div> <%= link_to_add_fields "Add Part", f, :part %> <div class="actions"> <%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %> </div> <% end %>
其实我的模特是:
“ItemPart”模型:
class ItemPart < ActiveRecord::Base belongs_to :item belongs_to :part end
“物品”型号:
class Item < ActiveRecord::Base belongs_to :item_type has_many :item_parts has_many :parts, :through => :item_parts accepts_nested_attributes_for :item_parts, :allow_destroy => true after_save :save_id validates :title, :presence => true def save_id item_part_attributes = [ { :item_id => self.id } ] end end
“部分”模型:
class Part < ActiveRecord::Base has_one :part_type has_many :item_parts has_many :items, :through => :item_parts end
我正在这样做的错误:
undefined method `klass' for nil:NilClass Extracted source (around line #26): 23: <%= render "item_part_fields", :p => parts_form %> 24: <% end %> 25: </div> 26: <%= link_to_add_fields "Add Part", f, :item %> 27: <div class="actions">
应用跟踪
app/helpers/application_helper.rb:44:in `link_to_add_fields' app/views/items/_form.html.erb:26:in `block in _app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569' app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569' app/views/items/edit.html.erb:3:in `_app_views_items_edit_html_erb___1857878264245794505_2169270380_890290209246324491'
Application_helper.rb
def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new # error line :44 fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f => builder) end link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" ) end
I'm using nested_attributes and trying to implement the add/remove fields on-the-fly throu ajax following Ryan Bates screencast about Nested Model (#196)
Doing this, it won't work, but when removing the "link_to_add_fields" line, it works fine.
The problem is that I'm don't know if I doing this all associations right.
<%= form_for(@item) do |f| %> <% if @item.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2> <ul> <% @item.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :item_type_id %><br /> <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %> <br /> <%= f.fields_for :item_parts do |parts_form| %> <%= render "item_part_fields", :p => parts_form %> <% end %> </div> <%= link_to_add_fields "Add Part", f, :part %> <div class="actions"> <%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %> </div> <% end %>
Actually my models are:
"ItemPart" model:
class ItemPart < ActiveRecord::Base belongs_to :item belongs_to :part end
"Item" model:
class Item < ActiveRecord::Base belongs_to :item_type has_many :item_parts has_many :parts, :through => :item_parts accepts_nested_attributes_for :item_parts, :allow_destroy => true after_save :save_id validates :title, :presence => true def save_id item_part_attributes = [ { :item_id => self.id } ] end end
"Part" model:
class Part < ActiveRecord::Base has_one :part_type has_many :item_parts has_many :items, :through => :item_parts end
The error I'm getting doing this way:
undefined method `klass' for nil:NilClass Extracted source (around line #26): 23: <%= render "item_part_fields", :p => parts_form %> 24: <% end %> 25: </div> 26: <%= link_to_add_fields "Add Part", f, :item %> 27: <div class="actions">
Application trace
app/helpers/application_helper.rb:44:in `link_to_add_fields' app/views/items/_form.html.erb:26:in `block in _app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569' app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569' app/views/items/edit.html.erb:3:in `_app_views_items_edit_html_erb___1857878264245794505_2169270380_890290209246324491'
Application_helper.rb
def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new # error line :44 fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f => builder) end link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" ) end
原文:https://stackoverflow.com/questions/6939634
满意答案
我想你需要一个不同的背景颜色,这就是为什么
border-radius: 25px 0 0 25px;
不适合你。 一种解决方案是选择背景并更改其渐变。 我已使用http://www.cssmatic.com/gradient-generator生成颜色渐变。.ui-state-highlight, .ui-widget-content .ui-state-highlight, .ui-widget-header .ui-state-highlight { border: 0; color: #ffffff; border-radius: 25px; text-align: center; } .ui-datepicker td { border: 0; padding: 0px; } //Create the gradient for the td here-- only for the selected date //At the end date, reverse the gradient and it will look almost like the picture you shared. .ui-datepicker-today { background: rgba(105,84,68,1); background: -moz-linear-gradient(left, rgba(105,84,68,1) 47%, rgba(173,217,120,1) 100%); background: -webkit-gradient(left top, right top, color-stop(47%, rgba(105,84,68,1)), color-stop(100%, rgba(173,217,120,1))); background: -webkit-linear-gradient(left, rgba(105,84,68,1) 47%, rgba(173,217,120,1) 100%); background: -o-linear-gradient(left, rgba(105,84,68,1) 47%, rgba(173,217,120,1) 100%); background: -ms-linear-gradient(left, rgba(105,84,68,1) 47%, rgba(173,217,120,1) 100%); background: linear-gradient(to right, rgba(105,84,68,1) 47%, rgba(173,217,120,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#695444', endColorstr='#add978', GradientType=1 ); }
Finally done this only with css, see FIDDLE here One need to play a bit with
position
andafter
.$("#datepicker").datepicker();
.ui-state-highlight, .ui-widget-content .ui-state-highlight, .ui-widget-header .ui-state-highlight { border: 0; border-radius: 25px; text-align: center; } td { position: relative; z-index: 1111; } a.ui-state-default.ui-state-highlight:after { content: ""; background: green; height: 23px; width: 25px; position: absolute; z-index: -1; right: 0; top: 1px; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <div class="demo"> <p>Date: <input id="datepicker" type="text"> </p> </div> <!-- End demo --> <div style="display: none;" class="demo-description"> <p>The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.</p> </div> <!-- End demo-description --> </body> </html>
相关问答
更多border-radius + background-color ==裁剪边框(border-radius + background-color == cropped border)
CSS在嵌套div上出现border-radius错误(CSS wrong appearance of border-radius on a nested div)
背景颜色重叠border-radius [关闭](Background-color overlapping border-radius [closed])
背景颜色隐藏边界半径ie8(background color hide border-radius ie8)
css中的border-radius和background-color冲突(border-radius and background-color conflict in css)
Internet Explorer css问题:border-radius,background-color,gradient(Internet Explorer css issues: border-radius, background-color, gradient)
CSS border-radius:20px(CSS border-radius:20px)
CSS border-radius(CSS border-radius)
如何在nativescript中给stacklayout赋予border和border-radius(how to give border and border-radius to stacklayout in nativescript)
未应用css border-radius(css border-radius is not applied)
相关文章
更多Storm 中 Fields作用
Becoming a data scientist
Solr: a custom Search RequestHandler
Create a Bootable MicroSD Card
Storm【设计细节】 -Fields对象
Solr 4.0: Partial documents update
用A标签实现页面内容定位 点击链接跳到具体位置
Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years
A5营销访谈:卢松松和你聊新媒体运营那些事
《wordpress插件制作视频教程》(How to create a wordpress plugin)全5集更新完毕[HDTV]
最新问答
更多python的访问器方法有哪些
使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
透明度错误IE11(Transparency bug IE11)
linux的基本操作命令。。。
响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
元素上的盒子阴影行为(box-shadow behaviour on elements)
它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640
Copyright ©2023
peixunduo.com All
Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!