Rails 3 重构问题

发布于 2024-10-10 16:09:07 字数 801 浏览 4 评论 0原文

以下视图代码生成一系列带有总计的链接(如预期):

<% @jobs.group_by(&:employer_name).sort.each do |employer, jobs| %>
    <%= link_to employer, jobs_path() %> <%= "(#{jobs.length})" %>
<% end %>

但是,当我重构视图的代码并将逻辑移至帮助器时,代码无法按预期工作。

view:

<%= employer_filter(@jobs_clone) %>

helper:

def employer_filter(jobs)
    jobs.group_by(&:employer_name).sort.each do |employer,jobs|
        link_to employer, jobs_path()
    end   
end

生成以下输出:

<Job:0x10342e628>#<Job:0x10342e588>#<Job:0x10342e2e0>Employer A#<Job:0x10342e1c8>Employer B#<Job:0x10342e0d8>Employer C#<Job:0x10342ded0>Employer D#

我不明白什么?乍一看,代码似乎是等效的。

The following view code generates a series of links with totals (as expected):

<% @jobs.group_by(&:employer_name).sort.each do |employer, jobs| %>
    <%= link_to employer, jobs_path() %> <%= "(#{jobs.length})" %>
<% end %>

However, when I refactor the view's code and move the logic to a helper, the code doesn't work as expect.

view:

<%= employer_filter(@jobs_clone) %>

helper:

def employer_filter(jobs)
    jobs.group_by(&:employer_name).sort.each do |employer,jobs|
        link_to employer, jobs_path()
    end   
end

The following output is generated:

<Job:0x10342e628>#<Job:0x10342e588>#<Job:0x10342e2e0>Employer A#<Job:0x10342e1c8>Employer B#<Job:0x10342e0d8>Employer C#<Job:0x10342ded0>Employer D#

What am I not understanding? At first blush, the code seems to be equivalent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-10-17 16:09:07

在第一个示例中,它直接输出到 erb,在第二个示例中,它返回该方法的结果。

试试这个:

def employer_filter(jobs)
    employer_filter = ""
    jobs.group_by(&:employer_name).sort.each do |employer,jobs|
        employer_filter += link_to(employer, jobs_path())
    end   
    employer_filter
end

然后在视图中这样调用它:

raw(employer_filter(jobs))

还要注意“raw”的使用。一旦你将字符串的生成移出模板,你需要告诉rails你不希望它被html转义。

为了额外的好处,您可以使用“注入”命令而不是显式构建字符串,但我很懒,想给您我知道无需测试即可工作的内容。

In the first example, it is directly outputting to erb, in the second example it is returning the result of that method.

Try this:

def employer_filter(jobs)
    employer_filter = ""
    jobs.group_by(&:employer_name).sort.each do |employer,jobs|
        employer_filter += link_to(employer, jobs_path())
    end   
    employer_filter
end

Then call it like this in the view:

raw(employer_filter(jobs))

Also note the use of "raw". Once you move generation of a string out of the template you need to tell rails that you don't want it html escaped.

For extra credit, you could use the "inject" command instead of explicitly building the string, but I am lazy and wanted to give you what I know would work w/o testing.

断肠人 2024-10-17 16:09:07

这个语法正如我所希望的那样工作:

def employer_filter(jobs_clone)
    jobs_clone.group_by(&:employer_name).sort.collect { |group,items|
        link_to( group, jobs_path() ) + " (#{items.length})"
    }.join(' | ').html_safe
end

This syntax worked as I hoped it would:

def employer_filter(jobs_clone)
    jobs_clone.group_by(&:employer_name).sort.collect { |group,items|
        link_to( group, jobs_path() ) + " (#{items.length})"
    }.join(' | ').html_safe
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文