formtastic - how to prepopulate a string input with a value

谁说我不能喝 提交于 2019-11-30 12:30:21

问题


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

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 -%>

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

Suggestions anyone?


回答1:


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] }



回答2:


sometimes the easiest things come to your mind by asking for them:

approach: do the job in the controller not the view!

if params[:email]
  @user = User.new(:email => params[:email])
else
  @user = User.new
end

sorry for bothering you!



来源:https://stackoverflow.com/questions/2044497/formtastic-how-to-prepopulate-a-string-input-with-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!