Some changes to Merb edge you should probably know about.
While doing a fresh pull of Merb edge this weekend I realized that all of my forms had suddenly just disappeared and I was also receiving some NoMethod errors; which is never a fun scenario. Just in case you like to develop on edge (and who doesn’t when it comes to Merb?) you should probably be aware of a few changes that went through this week.
There was a pretty big refactoring with the form_helpers. In fact, it’s still a little flaky at the time of this writing.
UPDATE: It has stabilized over the next couple of days and, according to wycats and GMFlash, should become more solid by the next release (0.9.5 at the time of this writing).
Previously in merb there were:
- “_control” controls* – These controls signified that it was going to be “bound” to a specific attribute on a model.
- “_field” controls* – These were controls which were not bound to a model.
This refactor completely eliminates all of that complexity. Now there is only one way helper for each control. One helper to bring them all and in the darkness bind them. Sorry I had to do it.
Here are a list of the new helper methods:
- form (previously this was form_tag)
- form_for (This helper will generate a form that is bound to a specific resource (model)
- button
- submit (previously submit_button)
- text_field, password_field, hidden_field file_field, text_area, select, check_box, radio_button, and radio_group
- The ‘form’ and ‘form_for’ methods now render output so you’ll need to prefix it with a %= rather than % and = rather than – if you’re using HAML.
The way you bind specific attributes to a model is by simply passing a symbol in to the method. You can see this in the private bound? method in the helpers.rb file.
private
def bound?(*args)
args.first.is_a?(Symbol)
end
Thank you, sweet baby jesus and GMFlash, for this refactor. It really does simplify views that require forms and makes things much cleaner. Suddenly the form helpers just make much more sense to me.
At the time of this writing there are a couple patches in the pipe:
- There a patch in the intertubes that changes the way ‘select’ works. Here is the ticket and the patch. UPDATE: This has been merged in to the refactoring.
Apparently there is a bug in the way the checkbox control sends it’s output that does not allow a checked checkbox to ever render a ‘true’ (1) value.UPDATE: My patched was merged in. You can see the ticket (and patch) here at Lighthouse.
Examples
Form Helper (not bound to a model)
<%= form :action => url(:login) do %>
<%= text_field :name => "login", :label => "Username" %>
<%= text_field :name => "password", :label => "Password" %>
<%= submit "Log in" %>
<% end =%>
Form Helper (bound to a model)
<%= form_for @post, :action => url(:posts) do %>
<%= text_field :title, :label => "Title" %>
<%= text_area :body, :label => "Body" %>
<%= check_box :published %>
<%= submit "Create or Update" %>
<% end =%>
- Notice the %= at the beginning and end of the form and form_for methods.
- Notice the :symbol being passed in to the control helper methods to signify a “bound” control.
select helper (bound)
select :foo, :collection => [['0', 'Text Method 1'], ['1', 'Text Method 2' ]]
which should generate something like this:
<select name="foo">
<option value="0">Text Method 1</option>
<option value="1">Text Method 2</option>
</select>
The above code didn’t go through the Ruby interpreter at all. It was all hand typed so please forgive me if there are typos.
Important Notes
When using the new helper methods to bind to your models. Please note that your models are required to have an errors methods; This error method should be provided by your ORM of choice (DM or AR).
So, if your models have:
include DataMapper::Resource
then all you need to do in your init.rb file is:
dependency "dm-validations"
As always there is much more that these helper methods are capable of, but it will take some digging around to find. You can always check out the merb documentation, stop by the #merb channel, or dig in to the merb code yourself!
I’ve heard a lot about Twitter’s
I think it’s too aggressive. Testing it in development, my comments often got detected as spam, despite the fact they were quite unspammy (in some cases, even blank [?]). I had to comment the lines that insta-banned posts that were identified as spam, because I ended up banning myself. If my comments got identified as spam then I imagine some of the crappy posts other users make will be.
Mollom’s website says that anything identified as spam is almost always spam, but…
I considered using Akismet but I like how Mollom provides captcha for content it’s not sure about. That’s pretty awesome in theory.
1908.txt;1;1
1908.txt;1;1
Hey man,
Sure do. I had it private for a while, because I had some pretty hard coded servers names and stuff in the deployment, but it should be good now.
http://github.com/bryansray/smerb/tree/master
It’s really rough right now. Feather is probably an excellent ‘real’ blog engine, but I like to have a personal sandbox.
Got a git repo so I can clone and take a look?
Testing out the new mollom spam protection.