简体   繁体   English

如何在Rails上的ruby中访问rails助手和嵌入资产javascript文件内的ruby?

[英]How do I access rails helpers and embedded ruby inside assets javascript files in ruby on rails?

I have a show and hide div that shows and hides comments when clicked. 我有一个显示和隐藏div,它在单击时显示和隐藏注释。 By default before it's clicked it shows a message count plus some down arrows eg 默认情况下,单击之前它会显示消息计数以及一些向下箭头,例如

"55 Comments [down arrows]" “ 55条评论[向下箭头]”

and when clicked it changes to: 单击后变为:

"Hide comments [up arrows]" “隐藏评论[向上箭头]”

I use this code for displaying the amount of comments by default and the arrow (users/new): 我使用此代码默认显示评论数量和箭头(用户/新用户):

    <span class="commentsCount"><%= pluralize(m.comments.count, 'comment') %></span>
    <span class="commentArrows"><%= image_tag 'view_all_comments.png' %> </span>

I use JQuery to help achieve the changes (users/load_comments.js.erb): 我使用JQuery来帮助实现更改(users / load_comments.js.erb):

$('.postHolder').off().on('ajax:success', '.view_all_comments', function(){

    var postContent = $(this).parents('.post_content'),
        commentContainer = postContent.find('.comment_container'),
        getComments = "<%= j render 'users/partials/show_all_comments' %>";
        commentContainer.slice(0, -2).remove();

// The next 2 lines replace the comment count with "Hide comments" and up arrows"

          postContent.find('.commentsCount').html("Hide comments");
          postContent.find(".commentArrows img").prop("src", "/assets/up_arrows.png");

             postContent.find('.comment_container:first').before(getComments);
});

Now the important part is when Hide comments is clicked again. 现在重要的部分是当再次单击“ 隐藏注释”时。 I need it to revert back to showing comments count. 我需要它来恢复显示评论数。 I can revert the arrows back to original arrows but the text I don't see a way of reverting back to the comments account because I can't access rails helpers and my instance variables etc inside my assets/javascripts/users.js file. 我可以将箭头还原为原始箭头,但文本中看不到还原为注释帐户的方法,因为我无法访问asset / javascripts / users.js文件中的rails助手和实例变量等。

Here is the code (assets/javascripts/users.js: 以下是代码(assets / javascripts / users.js:

    // for showing and hiding comments

        $('.expandComments').click(function() {

        var showAllCommentsSubmit = $(this).find('.showAllCommentsSubmit'),
            postContent = $(this).parents('.post_content'),
            commentContainer = postContent.find('.comment_container');


            if ($(this).hasClass('visible')) {
                postContent.find(".comment_container").slice(0, -1).hide();
                postContent.find(".commentArrows img").prop("src", "/assets/view_all_comments.png");

//Here is where I need the code to replace the "Hide comments" text.

            }  else {


                if (showAllCommentsSubmit.is(':disabled')) {
                    postContent.find(".comment_container").show();
                    postContent.find(".commentArrows img").prop("src", "/assets/hide_comments.png");

                } else {
                    showAllCommentsSubmit.trigger('submit');
                    showAllCommentsSubmit.prop({disabled: true});
                    postContent.find(".commentArrows img").prop("src", "/assets/comments-loader.gif");
                }
            }

            $(this).toggleClass('visible');

        });

Since I don't think it's possible to use embedded ruby / rails helpers in the assets javascript files how would I be able to achieve what I'm trying to achieve. 由于我认为无法在资产javascript文件中使用嵌入式ruby / rails帮助器,因此我将如何实现我要实现的目标。

Is there some way to display some html on top of other html then remove it when it's not needed any more and have the html underneath revealed again? 有什么方法可以在其他html之上显示一些html,然后在不再需要它时将其删除,并再次显示下面的html?

I'm sure there are some other solutions I can come up with such as bring some css into play.. having a div hide and show but I haven't figured how I'd do that exactly. 我敢肯定,我可以提出其他一些解决方案,例如发挥一些CSS的作用..具有div的隐藏和显示功能,但我还没有弄清楚该怎么做。 Wondering if there is a quick solution. 想知道是否有快速解决方案。

You can use Rails helpers in asset code. 您可以在资产代码中使用Rails帮助器。 You essentially just need to rename your asset from users.js to users.js.erb , and Rails will preprocess it with ERb before emitting the content as JavaScript. users.js users.js.erb ,您只需要将您的资产从users.js重命名为users.js.erb ,Rails会在将内容作为JavaScript发出之前使用ERb对其进行预处理。

Check out the Rails guide on preprocessing in the asset pipeline for details. 有关详细信息,请查阅关于资产管道中的预处理Rails指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM