[英]Why are parentheses mandatory when using route helper methods?
知道這在Ruby中是可行的:
method_name param, other_method other_param
這將是另一種編程語言中的等價物:
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
錯誤。
method_name param, other_method other_param
在Ruby中是不可能的,所以路由助手也不可能,因為它不明確。
在Matz的The Ruby Programming Language中甚至還有一節介紹這一點。
例:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.