知道在Ruby中这是可能的:
method_name(param, other_method(other_param))
在另一种编程语言中,它将等同于:
<%= link_to ticket.subject, ticket_path(ticket.id) %>
为什么使用自动生成的路径助手不可以做到这一点,这些方法就像其他方法一样?
例如:
<a href="/tickets/1">Lorem ipsum.</a>
有效 - 例如,它返回<%= link_to ticket.subject, ticket_path ticket.id %>
,但是:
unexpected tIDENTIFIER
不是 - 它返回AddFile
错误。
答案 0 :(得分:3)
method_name param, other_method other_param
在Ruby中是不可能的,因此路由助手无法做到这一点,因为它不明确。
Matz的“Ruby编程语言”中甚至还提到了section。
示例:
irb(main):001:0> def link_to(a, b)
irb(main):002:1> puts a, b
irb(main):003:1> end
:link_to
irb(main):004:0> def foo(a)
irb(main):005:1> puts 'foo'
irb(main):006:1> end
:foo
irb(main):007:0> link_to 'hello', foo 'abc'
SyntaxError: (irb):7: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
link_to 'hello', foo 'abc'
^
from /usr/local/var/rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):008:0> link_to 'hello', foo('abc')
foo
hello
nil