formtastic - 如何使用值预填充字符串输入 [英] formtastic - how to prepopulate a string input with a value

查看:34
本文介绍了formtastic - 如何使用值预填充字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用,用户只需在其中留下电子邮件即可发表评论.

I'm building an app, where users can give comments by just leaving their email to the comment.

我希望他们能够直接从那里注册,通过将他们的电子邮件地址作为参数的链接
(例如:<%= link_to register_path(:email => @comment.email %> ) - 所以还没有现有的用户记录.
这应该通过通过 :value 选项将值应用到 form.input 字段来完成.

I want them to be able to register directly from there, by a link with their email adress as a param
(like: <%= link_to register_path(:email => @comment.email %> ) - so there is no existing user record yet.
this should be done by applying a value to the form.input field via the :value option.

但是下面的代码不起作用!

But the following code doesn't work!

<%- if params[:email] -%>
  <%= f.input :email, :required => true, :value => params[:email] %>
<%- else -%>
  <%= f.input :email, :required => true %> 
<%- end -%>

我查看了 formtastic rdoc 和本地 gem 的内部,但找不到任何选项.

I had a look inside the formtastic rdoc and inside my local gem but couldn't find any option for that.

给任何人建议?

推荐答案

是的,你自己得到了正确的答案!Formtastic 的 rdoc 一团糟,但我们正在努力.如果您只想在视图级别进行此操作,另一种选择是使用 :input_html 选项,它使您可以直接访问输入标签的 HTML 属性:

Yup, you got the right answer all by yourself! Formtastic's rdoc is a mess, but we're working on it. Another option if you want this purely at a view level is to use the :input_html option, which gives you direct access to HTML attributes of the input tag:

f.input :email, :input_html => { :value => params[:email] }

这篇关于formtastic - 如何使用值预填充字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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