简体   繁体   English

rails自定义html元素表单

[英]rails custom html elements form

I am new in Rails and i've searched around the web and books but I couldnt find how to make custom forms when working with rails.. what happens is that I am used to write my own HTML tags and I confortable with that. 我是Rails的新手,我搜索过网络和书籍,但我在使用rails时无法找到如何制作自定义表单。会发生的是,我习惯于编写自己的HTML标签,并且我对此感到很满意。 I dont like to use libs like JSF (from JAVA) that writes html components for me and I dont want that rails write it for me, except for simple tags like 我不喜欢使用像JSF(来自JAVA)的libs为我编写html组件,我不希望rails为我编写它,除了像我这样的简单标签

text_field(:post, :title, :size => 20)
# => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />

so.. how can I do that.. for example: I would like to write by myself 所以..我怎么能这样做..例如:我想自己写

<input type="text" class="myclass" data="mydata" name="how-to-get-the-attribute-name-with-rails" value="how-to-get-value-with-rails" />

how can I do that? 我怎样才能做到这一点?

If you want more control over the html you are creating, you can also use a content_tag 如果您想要更多地控制您正在创建的html,您还可以使用content_tag

content_tag :input, "label_name", class: "myclass", data: "mydata", name: "how-to-get-the-attribute-name-with-rails", value: "how-to-get-value-with-rails"

You can supplement any html element tag for :input . 您可以为:input补充任何html元素标记。 So if you want a div instead, use :div etc... 所以如果你想要一个div,请使用:div等...

As you have written above, The name of any form field in rails is in the following format 如上所述,rails中任何表单字段的名称采用以下格式

name = 'post[name]' i.e. model_name[attribute_name]

So your params hash will contain :post => {:name => value} which allows you to use mass-assignment. 所以你的params哈希将包含:post => {:name => value} ,它允许你使用质量赋值。 But if you want to get some extra parameters from the form you can directly include them in the form and give them any name as you want. 但是如果你想从表单中获取一些额外的参数,你可以直接将它们包含在表单中,并根据需要给它们任意名称。 It will be available in your params hash. 它将在你的参数哈希中提供。


You can get value easily using value = <%= @object.attribute_name. %> 您可以使用value = <%= @object.attribute_name. %>轻松获取值value = <%= @object.attribute_name. %> value = <%= @object.attribute_name. %> I am not sure if you wanted to know this or something else. value = <%= @object.attribute_name. %>我不确定你是否想知道这个或其他什么。 Let me know if you need more help. 如果您需要更多帮助,请告诉我。

You can simply write html inside a Rails form, so 您只需在Rails表单中编写html即可

<input type="text" class="myclass" data="mydata" name="how-to-get-the-attribute-name-with-rails" value="how-to-get-value-with-rails" />

is perfectly valid, but note that you should use the names and id's in correct way to automatically bind them to your back end controller. 完全有效,但请注意,您应该以正确的方式使用名称和ID来自动将它们绑定到后端控制器。

After all, what text_field(:post, :title, :size => 20) does is it convert the parameters to pure HTML (we call them helper methods in Rails). 毕竟, text_field(:post, :title, :size => 20)作用是将参数转换为纯HTML(我们称之为Rails中的辅助方法)。

Read here for formhelper options (using helpers will keep your code clean). 在这里阅读formhelper选项(使用帮助程序将保持您的代码清洁)。

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

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