[英]virtual model and form_for (or formtastic)
Sometimes we need form without model creation - for example search field or email, where should be send some instructions. 有时我们需要没有模型创建的表单 - 例如搜索字段或电子邮件,应该在哪里发送一些指令。 What is the best way to create this forms?
创建此表单的最佳方法是什么? Can i create virtual model or something like this?
我可以创建虚拟模型或类似的东西吗? I'd like to use formtastic, but not form_tag.
我想使用formtastic,但不是form_tag。
Firstly, Formtastic doesn't need a model in all cases, although it certainly works best and requires less code with a model. 首先,Formtastic在所有情况下都不需要模型,尽管它确实效果最好,并且需要较少的代码与模型。
Just like Rails' own built-in form_for
, you can pass in a symbol instead of an object as the first argument, and Formtastic will build the form and post the params based on the symbol. 就像Rails自己的内置
form_for
,你可以传入一个符号而不是一个对象作为第一个参数,而Formtastic将构建表单并根据符号发布params。 Eg: 例如:
<% semantic_form_for(:session) do |f| %>
...
<% end %>
This will make the form values available to your controller as params[:session]
. 这将使控制器可以使用
params[:session]
作为表单值。
Secondly, a model doesn't mean an ActiveRecord model. 其次, 模型并不意味着ActiveRecord模型。 What I mean is, Formtastic will work with any instance of a class that quacks like an ActiveRecord model.
我的意思是,Formtastic将与叫声也像ActiveRecord模型类的任何实例工作。
A classic example of this that many people are using Authlogic for authentication with Formtastic. 许多人使用Authlogic进行Formtastic身份验证的典型例子。 Part of Authlogic is the idea of a UserSession model, which works fine:
Authlogic的一部分是UserSession模型的想法,它工作正常:
Controller: 控制器:
def index
@user_session = UserSession.new
end
Form: 形成:
<% semantic_form_for(@user_session) do |f| %>
<%= f.input :login %>
<%= f.input :password %>
<% end %>
This will make your form data available in your controller as params[:user_session]
. 这将使您的控制器中的表单数据可用作
params[:user_session]
。
It's really not that hard to create a model instance to wrap up the concerns of your model. 这真的不是很难创建一个模型实例来包裹你的模型的关注。 Just keep implementing the methods Formtastic is expecting until you get it working!
只需继续实施Formtastic期待的方法,直到你开始工作!
default_language.rb default_language.rb
class DefaultLanguage attr_accessor :language_id end
foo_controller.rb foo_controller.rb
def index @default_language = params[:default_language] || Language.find_by_name("English") end
index.erb index.erb
<% semantic_form_for @default_language do |form| %> <% form.inputs :id => 'default_language' do %> <%= form.input :id, :as => :select, :collection => @languages, :required => false, :label => "Primary Language:", :include_blank => false %> <% end %> <% end %>
I used AJAX to post the form when the value changed. 当值改变时,我使用AJAX发布表单。
Or you simply create a form with form_for and leave the model reference blank. 或者您只需使用form_for创建一个表单,并将模型引用留空。 for example
<% form_for "", :url=>some_url do |f| %>
例如
<% form_for "", :url=>some_url do |f| %>
<% form_for "", :url=>some_url do |f| %>
<%= f.text_field "some_attribute" %>
<%= submit_tag "submit" %>
<% form_for "", :url=>some_url do |f| %>
<%= f.text_field "some_attribute" %>
<%= submit_tag "submit" %>
You can fetch the values by simply saying params[:some_attribute] in your controller. 您只需在控制器中输入params [:some_attribute]即可获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.