Giter Site home page Giter Site logo

virtus-multiparams's Introduction

Virtus::Multiparams

Support for Rails-style multiparameters, which makes datetime_select and friends work with Virtus objects.

Rails Date, Time and DateTime selectors all use multiparameters. This means they break one attribute into separate parameters for each input, like year, month and day, and reconstitute these multiple parameters into a single parameter to be passed back into a model as an attribute.

Usage

class PostForm
  include Virtus
  include Virtus::Multiparams

  attribute :title, String
  attribute :publish_at, DateTime
end

# app/views/posts/new.html.erb
<%= form_for @post_form do |form| %>
  <%= form.label :title %>
  <%= form.text_field :title %>

  <%= form.label :publish_at %>
  <%= form.datetime_select :publish_at %>
  <!-- Which does something like:
    <select name="post_form[publish_at(i1)]"><option>2015</option>...</select>
    <select name="post_form[publish_at(i2)]"><option value="1">January</option>...<option value="12">December</option></select>
    <select name="post_form[publish_at(i3)]"><option>1</option>...</select>
    <select name="post_form[publish_at(i4)]"><option>1</option>...<option>24</option></select>
    <select name="post_form[publish_at(i5)]"><option>1</option>...<option>60</option></select>
    <select name="post_form[publish_at(i6)]"><option>1</option>...<option>60</option></select>
  -->

  <%= form.submit %>
<% end %>

# app/controllers/posts_controller.rb
class PostsController
  def new
    @post_form = PostForm.new
  end

  def create
    @post_form = PostForm.new(params[:post_form])
    # ...
  end
end

virtus-multiparams's People

Contributors

sj26 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

virtus-multiparams's Issues

TimeZone LOL

Hi,
yup right before you set time in the code, a comment says : # Also, lololol timezones.

And indeed !!... the params my user would send from Paris to the server in London would be interpreted as the time in London. And my user would get confused because he would say reservation at 10:00 and it'd come back to him at 9:00 although in the DB it would be recorded as 8:00 (which is UTC).

The solution is to set time with timezone: Time.zone.local(2015, 12, 25, 12, 0) so everyone can hear the bells jingle in sync :-).

Of course that means being in the context of Rails. But wasn't it at the first place ?
Cheers,
Charly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.