Ruby on Rails - 带括号的 link_to [英] Ruby on Rails - link_to with parentheses

查看:32
本文介绍了Ruby on Rails - 带括号的 link_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,这是一个新手问题.

Sorry, but this is a newbie question.

已经有很多关于link_to"的问题,但没有人回答我的问题,我认为这不是link_to特有的...

There are also a lot of questions on "link_to" already, but none answer my question, which I don't think is really specific to link_to...

创建这样的链接有效:

<%= link_to person.automobile_id, person %>

但是,像这样尝试不起作用:

But, trying it like this does not work:

<%= link_to (translation.request_id, translation) %>

这会产生一个错误:

syntax error, unexpected ',', expecting ')'

这让我很困惑,因为在我看来,我只是在 link_to 的参数周围加上括号.

This confuses me, since to me it looks like I'm just wrapping parenthesis around the arguments for link_to.

我误解了什么?

推荐答案

在大多数情况下,方法参数的括号在 Ruby 中是可选的.

In most cases the parentheses to a method argument is optional in Ruby.

link_to("link name", path) 

相当于

link_to "link name", path

后一个例子在 Rails 社区中更为惯用.

The latter example is more idiomatic in the Rails community.

但是,必须保留间距.如果省略括号,则方法和参数之间只能有一个空格.对于刚接触 Ruby 和 Rails 的人来说,它可能看起来有点奇怪,但它的使用非常普遍,您很快就会习惯它.

However, the spacing must be preserved. if you omit the parenthesis then there can only be one space between the method and argument. It may look a little strange to someone new to Ruby and Rails, but is used so prevalently you will get used to it quick.

例外情况是,如果您将方法链接在一起并且较早的方法之一有参数,则您需要包含括号.

The exception is if you are chaining methods together and one of the earlier methods has an argument then you will need to include the parentheses.

这篇关于Ruby on Rails - 带括号的 link_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆