简体   繁体   English

Ruby on Rails语法混淆

[英]Ruby on Rails Syntax Confusion

I am starting to learn Rails and I am confused as heck. 我开始学习Rails,并且感到困惑。

<%= form_for :story, url:'create' do |f| %>
<p>
    <%= f.label :title %>
</p>

<p>
    <%= f.submit %>
</p>

<% end %>

So, the way I understand is that form_for is a method which takes a model. 因此,据我了解,form_for是采用模型的方法。 I pass in the :story. 我通过:story。 Even if I replace the :story with :post it will work as expected. 即使我将:story替换为:post,它也将按预期工作。

The url is set as url: instead of :url (Why colon after works and colon before does not) url设置为url:而不是:url(为什么后面的冒号有效,而前面的冒号无效)

The following does not work: 以下内容不起作用:

<%= form_for @story, {url => "create"} do |f| %>

This has to do with the initialize method of the ActionView::FormHelper 这与ActionView :: FormHelperinitialize方法有关

:story is a symbol . :story是一个符号 In ruby, symbols which are essentially constants are expressed with a colon before the string. 在ruby中,本质上是常量的符号在字符串之前用冒号表示。

which creates 这创造了

<form action="/story" method="post">

url: 'create' is a ruby hash which uses symbols as keys and can be written as url: 'create'是使用符号作为键的红宝石哈希 ,可以写为

{url: create} or { :url => "create"}

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

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