[英]Issue with <ol> <li> implementation with rails partial and cocoon gem
I'm having some trouble with implementing an ordered list in my rails application. 我在rails应用程序中实现有序列表时遇到了一些麻烦。 I've been using the cocoon gem for a some nested forms and everything's working functionality wise, however, i cant seem to render an ordered list to make the nested form more readable.
我一直在使用cocoon gem来创建一些嵌套的表单,并且所有功能都是明智的,但是,我似乎无法呈现一个有序列表来使嵌套表单更具可读性。
to start my code looks like this _form.html.erb 启动我的代码看起来像这个_form.html.erb
....
<div class="clearfix">
<div class="col-lg-3 col-md-6">
<ol>
<%= f.label :Movement %>
<%= f.fields_for :movements do |movement| %>
<li><%= render 'movement_fields', f: movement %></li>
<% end %>
<div class="links">
<%= link_to_add_association 'Add movement', f, :movements, class: "pull-right d-inline-block btn btn-primary add-button" %>
</ol>
</br>
</br>
</div>
</div>
.....
_movement_fields.html.erb _movement_fields.html.erb
<div class="nested-fields">
<li><%= f.text_field :name %></li>
<%= link_to_remove_association "Remove", f, class: "pull-left d-inline-block form-button btn btn-warning" %>
</div>
exercise.js exercise.js
$(document).ready(function() {
$('#exercise_workout_date').datepicker({ dateFormat: 'yy-mm-dd' });
new Morris.Line({
// ID of the element in which to draw the chart.!
element: 'chart',
// Chart data records -- each entry in this array corresponds to a point on!
// the chart.!
data: $('#chart').data('workouts'),
// The name of the data record attribute that contains x-values. xkey: 'workout_date',!
// A list of names of data record attributes that contain y-values. ykeys: ['duration_in_min'],!
// Labels for the ykeys -- will be displayed when you hover over the // chart.!
xkey: 'workout_date',
ykeys: ['duration_in_min'],
labels: ['Duration (min)'],
xLabels: "day",
xLabelAngle: 60,
xLabelFormat: function (x) {
date_string = x.getFullYear() + "/" + parseInt(x.getMonth() + 1) + "/" + x.getDate();
return date_string;
},
yLabelFormat: function(y) { return y + ' min'; }
});
});
$('#list').html('<li>foo</li>');
application.js 的application.js
//= require jquery
//= require jquery-ui/datepicker
//= require jquery_ujs
//= require jquery.raty.min
//= require chosen-jquery
//= require bootstrap-sprockets
//= require raphael.min
//= require morris
//= require social-share-button
//= require html.sortable
//= require cocoon
// require turbolinks
//= require_tree .
application.html application.html
<!DOCTYPE html>
<html>
<head lang=”en”>
<title>FitnessLabs (A & K LABS)</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<%= favicon_link_tag 'logo.ico' %>
<link href="https://fonts.googleapis.com/css?family=Doppio+One|Pacifico" rel="stylesheet">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<meta name=viewport content="width=device-width, initial-scale=1">
</head>
<body>
<%= render "layouts/navigation2"%>
<div class="container-fluid">
<%= render 'layouts/messages'%>
<%= yield %>
</div>
<%= render "layouts/footer" %>
</body>
<script src="//load.sumome.com/" data-sumo-site-id="c5bf8251f90d125ef3264df7e976b4ab5a4a5a380ab1deaec05444284b2169d0" async="async"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
</html>
Currently this code renders a 1. for the first list item, however, when i click 'add movement' it doesn't render the 2. 3. 4. etc. for each successive part of the form. 目前,此代码为第一个列表项呈现1.但是,当我单击“添加运动”时,它不会为表单的每个连续部分呈现2. 3. 4.等。 I've tried moving the < ol> and < li>'s around but this is the closest i've come.
我试过移动<ol>和<li>,但这是我最接近的。
I want the user to be shown 1, 2, 3 etc. for each new nested form partial thats rendered, so it makes it clearer which coincides with which. 我希望为每个新呈现的嵌套表单部分显示1,2,3等用户,这样可以更清楚地与哪个一致。 How would i go about doing this?
我该怎么做呢? Am i on the right path?
我在正确的道路上吗?
Thanks, 谢谢,
Original: 原版的:
So the problem is that html before 5 doesn't render when your jquery fires to add the field. 所以问题是当你的jquery触发添加字段时,5之前的html不呈现。
The solution is open ended here, you use any number of technologies to solve the issue as it's not related to cocoon or rails really. 解决方案在这里是开放式的,您可以使用任意数量的技术来解决问题,因为它与cocoon或rails无关。
My suggestion is since you have cocoon already, just put 1 of the two options below in the custom cocoon callback files ( app/assets/javascript/
), you can use a after-insert callback to ... 我的建议是,既然你已经有了cocoon,只需将下面两个选项中的一个放入自定义cocoon回调文件(
app/assets/javascript/
)中,就可以使用后插入回调来...
Option A 选项A.
You use jquery to force ONLY the <OL>
list to render again, allowing the HTML5 to do the rest of the work for you. 您使用jquery强制仅重新呈现
<OL>
列表,允许HTML5为您完成剩余的工作。 Shown in this post ... how to "refresh" UL after adding new LI with jQuery? 在这篇文章中显示... 如何在使用jQuery添加新LI后“刷新”UL?
Option B 选项B.
Trigger a count of the fields & inject that number in to the <LI>
item ... shown on Stackoverflow: Using jQuery to dynamically refresh an ordered list 触发字段计数并将该数字注入到Stackoverflow上显示的
<LI>
项目中: 使用jQuery动态刷新有序列表
Update : 更新 :
I took a pretty long wack at the recipes_controller.rb
, the model & the views. 我在
recipes_controller.rb
,模型和视图上花了很长时间。 I had to turn off kicksearch - as it wasn't worth trouble shooting to just get the controller running fully. 我不得不关闭kicksearch - 因为让控制器完全运行不值得解决问题。 Non-comprehensive list of stuff ...
非全面的东西清单......
user_recipes.rb
) to handle look up better of recipe & users. user_recipes.rb
)来处理更好的配方和用户。 @recipe.user
& helps with the Rails Way (allowing you flexibility in the future for devise & pundit/cancancan/whatever you eventually use to restrict access to records & pages). @recipe.user
并帮助使用Rails方式(允许您在将来灵活设计和专家/ cancancan /您最终用于限制对记录和页面的访问的任何内容)。 .id
at the end of the current_user
, which we fixed after going to the @user
method current_user
的末尾没有.id
,我们在转到@user
方法后@user
is_owner
( recipes_helper.rb
) as there were 5 places you were testing it. is_owner
( recipes_helper.rb
)做了一个帮助,因为你有5个地方在测试它。 I also removed the admin? include RecipeHelper
at the top of the controller to make it work there. include RecipeHelper
以使其在那里工作。 user_id
from recipes, it looks like you renamed the forgein_key there to chef_id anyways ... which I wouldn't have done because it means the helper I made for is_owner
only works for recipe_controller instead of the whole app, & makes it more of a DSL versus easily being able to look at the thing. user_id
的验证,看起来你将forgein_key重命名为chef_id无论如何...我不会这样做因为它意味着我为is_owner
制作的is_owner
只适用于recipe_controller而不是整个应用程序,并使其更像是一个DSL,而不是容易看到的东西。 views/recipes/_gravatar_section.html.erb
) & passed the @user
in there as a local var views/recipes/_gravatar_section.html.erb
)做了一个部分并将@user
作为本地var传递给了 PS: I'm at the point now where the recipes run enough to test the jquery against ... will update more or you can watch the updates via the forked repo github , PS:我现在正处于配方运行足以测试jquery的地步......将更新更多或者你可以通过forked repo github观看更新,
Javascript Asset stuff ... Javascript资产的东西......
rails assets:clean
& the rails assets:precompile
which I thought I'd mention just incase I hadn't already. rails assets:clean
和rails assets:precompile
我认为我会提到的rails assets:precompile
只是因为我还没有。 //= require_tree .
//= require_tree .
absolutely must be at the bottom or you're going to have issues including all the files. //=
in front of them. //=
在它们前面。 app/assets/javascript/exercise.coffee
showing up in the directory & that sometimes interferes with the loading of the exercise.js
stuff. app/assets/javascript/exercise.coffee
显示在目录中,有时会干扰exercise.js
的加载。 Found out the datepicker going bad was a jquery conflict by brute force hand testing until I got a working version. 发现日期选择器变坏是一个jquery冲突蛮力手测试,直到我得到一个工作版本。
The culprit is one of these guys ... 罪魁祸首是其中一个......
//= require jquery-ui/datepicker
//= require jquery_ujs
//= require jquery.raty.min
//= require chosen-jquery
//= require bootstrap-sprockets
//= require raphael.min
//= require morris
//= require social-share-button
//= require html.sortable
I'd recommend slowly adding them one by one, in order of most trustworthy...till it breaks the javascript again. 我建议按照最值得信赖的顺序逐个添加它们......直到它再次打破javascript。 Your refresh of the ordering numbers should work if try it after pulling all the items above & the datepicker too (be sure to remove the datepicker script you put in your views) - read below for more on how trouble shoot these & fix them ...
如果在拉动上面的所有项目和日期选择器之后尝试它,那么您的订购号码刷新应该有效(请务必删除您在视图中添加的日期选择器脚本) - 请阅读以下内容,了解更多关于如何解决这些问题并解决它们的问题。 。
Rails - asset pipeline Link - Javascript trouble shooting stuff ... Rails - 资产管道链接 - Javascript解决问题......
elements
for your js file names ... saw the cocoon was loaded under the "elements" section elements
...看到cocoon加载在“elements”部分下 app/assets/javascript/exercise.coffee
showing up in the directory app/assets/javascript/exercise.coffee
出现在目录 //= require_tree .
//= require_tree .
... absolutely must be at the bottom or you're going to have issues including all the files. //=
in front of them. //=
在它们前面。 It's required to load them grep -r 'script' /app
& check any spot that shows up ... I'm finding scripts in your view files instead of the assets files for javascript ... If the asset asset is loading, we know it's either a conflict or poorly written code grep -r 'script' /app
并检查显示的任何位置...我在您的视图文件中找到脚本而不是资源文件javascript ...如果资产资产正在加载,我们知道它是冲突或编写得不好的代码 app/assets/javascript/application.js
, at the bottom of the file up the //= .
app/assets/javascript/application.js
,在文件的底部向上//= .
& save it ... ... now, you should re-add your js files to the app/assets/javascript
one at a time, testing each time by refreshing the ... ... webpage, possibly restarting the server after copying the file in & doing a rake or rails assets like shown below ... rails assets:clean
& the rails assets:precompile
app/assets/javascript
,每次刷新......网页进行测试,之后可能重新启动服务器复制文件并执行rake或rails资产,如下所示... rails assets:clean
&the rails assets:precompile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.