加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在formtastic中添加自定义输入字段?

发布时间:2020-12-17 03:20:33 所属栏目:百科 来源:网络整理
导读:我无法弄清楚,或找到一个非常简单的问题的任何解决方案: “我如何在formtastic中定义自己的输入字段?” 这就是我得到的: %= semantic_form_for @someFantasticVariable,:url = "/someFantasticUrl.html" do |f|% %= f.inputs do % %= f.input :something_
我无法弄清楚,或找到一个非常简单的问题的任何解决方案:
“我如何在formtastic中定义自己的输入字段?”

这就是我得到的:

<%= semantic_form_for @someFantasticVariable,:url => "/someFantasticUrl.html" do |f|%>  
    <%= f.inputs do %>
        <%= f.input :something_else_id,:required => true,:as => :select,:collection   => SomethingElse.find(:all),:label =>"The something else"%>
        <%= f.input :fantastic_max_cost,:label => "Budget (max cost)"%>  
    <%end%>


    <%= f.buttons do%>
        <%= f.commit_button :button_html => { :class => "primary",:disable_with =>     'Processing...',:id => "commitButton"}%>
    <%end%>
<%end%>

现在..

我想要一个非常简单的事情.我想要一个不属于该模型的字段.我想有一个日期字段,我可以用它来计算控制器中的一些东西.所以我想这样做:

<%= f.inputs do %>
    <%= f.input :something_else_id,:label =>"The something else"%>
    <%= f.input :fantastic_max_cost,:label => "Budget (max cost)"%>  
    <%= f.input :start_date,:as => :date,:label => "Start date"%>
<%end%>

但显然我不被允许,而且我无法通过我的谷歌搜索来找到任何办法.任何帮助/想法?

解决方法

如果您的某些属性不属于您的模型,那么模型上应该存在getter和setter:

def start_date
end

def start_date=(arg)
end

然后,您可以在控制器或任何您想要的任何地方计算您的员工:

...
puts params[:somefantasticvariable][:start_date]
...

但这是一个快速的形式化黑客,你应该找到一些更好的方法,比如一些css的非形式输入等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读