开发者

How to do a non-model form in ruby on rails?

I'm trying to make a non-model f开发者_如何学运维orm in ruby on rails, most of the examples I can find only have one field (say a search field) or use an old way of writing a form like this An Email Form with Ruby on Rails

If anyone could show me example code of a non-model form with say two fields for the view and how I access those fields in the controller I'd be grateful.

Thanks so much.


You will need FormHelper methods:

Say you want a simple test action that submits to do_test action:

A simple view for test action (posts/test.html.erb):

<% form_tag '/posts/do_test' do %>
    <%=label_tag 'name' %>
    <%=text_field_tag 'name'%>

    <%=label_tag 'phone' %>
    <%=text_field_tag 'phone'%>

    <div><%= submit_tag 'Save' %></div>
<% end -%>

In the posts controller:

def test
end

def do_test
  name = params[:name]
  phone = params[:phone]
  # do whatever you want...
end

Also you need to add these 2 actions to the routes in config/routes.rb

map.resources :posts, :collection=>{:test => :get, :do_test => :post}


If you create a class to represent your object (Let's call it ContactInfo) you can define methods on that class, then use them using the standard Rails form builder helpers.

class ContactInfo 
  attr_accessor :name, :company, :email, :phone, :comments

  def initialize(hsh = {})
    hsh.each do |key, value|
      self.send(:"#{key}=", value)
    end
  end
end

And in your form:

<h2>Contact Us</h2>
<% form_for(@contact_info, :url => path_for_your_controller_that_handles_this, :html => {:method => :post}) do |f| %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  ...
<% end %>

So far, who cares right?

However, add in the validatable gem, and you've got a real reason to do this! Now you can have validation messages just like a real model.

Check out my completed ContactInfo class:

class ContactInfo
  include Validatable

  attr_accessor :name, :company, :email, :phone, :comments

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :phone

  def initialize(hsh = {})
    hsh.each do |key, value|
      self.send(:"#{key}=", value)
    end
  end
end

I like this because you can write your controller much same way as an ActiveRecord object and not have to muss it up with a lot of logic for when you need to re-display the form.

Plus, if you're using Formtastic or another custom form builder, you can use this object with it, easily keeping your existing form styles.


From Rails 4 you can use ActiveModel::Model

http://blog.isnorcreative.com/2014/05/08/easier-non-activerecord-models-in-rails-4.html

class Contact
  include ActiveModel::Model

  attr_accessor :email, :message
  validates :email, presence: true, length: {in:2..255}

end


You can use the ActionView::Helpers::FormTagHelper for non-model use. Like

form_tag
text_field_tag
hidden_field_tag

Api has many good examples. http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html


Have you thought about using a presenter?

http://blog.jayfields.com/2007/03/rails-presenter-pattern.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜