Trouble with custom input mappings

84 views
Skip to first unread message

Reid

unread,
Feb 2, 2011, 10:38:41 AM2/2/11
to SimpleForm
I apologize if I've just missed something completely obvious. My app
uses optimistic locking via the :lock_version attribute. I have been
including this in all my forms as <%= f.input :lock_version %> which
obviously displays as an integer field. I know that I can just
append :as => :hidden but I'd like to make this DRY and set up a
custom input mapping.

In my simple_form.rb initializer I have added the line:
config.input_mappings = { /lock_version/ => :hidden }

This is not working. I have tested it with different fields and
different input types and none of it appears to work. Did I miss
something totally obvious?

Thanks for your help.

Rafael Mendonça França

unread,
Feb 2, 2011, 10:59:38 AM2/2/11
to plataformate...@googlegroups.com
Sorry but, did you restart the server?

I tried to reproduce it, but I did not succeed. I used this test:

swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
  with_form_for @user, :lock_version
  assert_no_select 'form input#user_lock_version.integer'
  assert_select 'form input#user_lock_version.hidden'
end

And it works.

Rafael Mendonça França

Carlos Antonio da Silva

unread,
Feb 2, 2011, 11:04:14 AM2/2/11
to plataformate...@googlegroups.com
Also, this feature is only available in SimpleForm 1.3 and master, which version are you using?
If you are using an older version, I'd recommend you to use from master instead of 1.3, there should be a new release soon.
--
At.
Carlos A. da Silva

Reid

unread,
Feb 2, 2011, 11:59:45 PM2/2/11
to SimpleForm
The server has been restarted a few times since I added the mappings
but just to be sure restarted it again with no luck. I was using
SimpleForm 1.3 but switched to master per your suggestion... still no
luck.

Incase I am really messing this up, here is my form:
<%= simple_form_for @project do |f| %>
...
<%= f.input :lock_version %>
<%= f.button :submit %>
<% end %>

The attribute is called lock_version and my config line is as above.
Thank you for your assistance... I know an easy fix would be to use
the :as => :hidden to each instance but the custom mapping would be so
much cleaner.

-Reid

On Feb 2, 10:04 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Also, this feature is only available in SimpleForm 1.3 and master, which
> version are you using?
> If you are using an older version, I'd recommend you to use from master
> instead of 1.3, there should be a new release soon.
>
> On Wed, Feb 2, 2011 at 1:59 PM, Rafael Mendonça França <rafael....@gmail.com
>
>
>
>
>
> > wrote:
> > Sorry but, did you restart the server?
>
> > I tried to reproduce it, but I did not succeed. I used this test:
>
> > swap SimpleForm, :input_mappings => { /lock_version/ => :hidden } do
> >   with_form_for @user, :lock_version
> >   assert_no_select 'form input#user_lock_version.integer'
> >   assert_select 'form input#user_lock_version.hidden'
> > end
>
> > And it works.
>
> > Rafael Mendonça França
> > Twitter:http://twitter.com/rafaelfranca
> > Blog:www.rafaelfranca.com.br
>

Rafael Mendonça França

unread,
Feb 3, 2011, 10:37:37 AM2/3/11
to plataformate...@googlegroups.com
I found the bug. In the actual code base the custom mappings only works for string attributes, as you can see here:


Rafael Mendonça França
Twitter: http://twitter.com/rafaelfranca
Blog: www.rafaelfranca.com.br


Carlos Antonio da Silva

unread,
Feb 3, 2011, 10:39:23 AM2/3/11
to plataformate...@googlegroups.com
Right, good catch. Thanks.

Rafael Mendonça França

unread,
Feb 3, 2011, 11:12:14 AM2/3/11
to plataformate...@googlegroups.com
This pull request fixes the bug. Please test the commit and report back if it works.


Rafael Mendonça França
Twitter: http://twitter.com/rafaelfranca
Blog: www.rafaelfranca.com.br


Carlos Antonio da Silva

unread,
Feb 3, 2011, 11:26:09 AM2/3/11
to plataformate...@googlegroups.com
Sounds good. 
Just a question for discussion: do you think custom types should have high or lower precedence over the default input types?

Rafael Mendonça França

unread,
Feb 3, 2011, 11:33:58 AM2/3/11
to plataformate...@googlegroups.com
I think that custom types should have high precedence.

If I'm setting a custom mapping for a attribute I really need it to use my custom mapping.

This was the case that I saw more people using. Like the Reid.

Rafael Mendonça França
Twitter: http://twitter.com/rafaelfranca
Blog: www.rafaelfranca.com.br


Carlos Antonio da Silva

unread,
Feb 3, 2011, 1:22:27 PM2/3/11
to plataformate...@googlegroups.com
I agree they should have higher precedence, hopefully it won't lead to any sort of confusion. Just wanted to put that under discussion =D.
I'll apply your patch later, just waiting Reid feedback. Thanks mate.

Reid

unread,
Feb 3, 2011, 8:14:59 PM2/3/11
to SimpleForm
Works perfectly! Thank you both so much.

On Feb 3, 12:22 pm, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> I agree they should have higher precedence, hopefully it won't lead to any
> sort of confusion. Just wanted to put that under discussion =D.
> I'll apply your patch later, just waiting Reid feedback. Thanks mate.
>
> On Thu, Feb 3, 2011 at 2:33 PM, Rafael Mendonça França <rafael....@gmail.com
> >>>>>https://github.com/plataformatec/simple_form/blob/master/lib/simple_f...
>
> >>>>> I will provide a patch to work with all types.
>
> >>>>> Rafael Mendonça França
> >>>>> Twitter:http://twitter.com/rafaelfranca
> >>>>> Blog:www.rafaelfranca.com.br
>

Carlos Antonio da Silva

unread,
Feb 3, 2011, 8:35:09 PM2/3/11
to plataformate...@googlegroups.com
Applied in master branch.
Thanks guys.
Reply all
Reply to author
Forward
0 new messages