Good Forms Helpers for Sinatra

Good forms helpers for Sinatra?

I find sinatra-formhelpers useful and used it in some projects. Have a look at their Github page, the code is pretty straightforward and might just be what you are looking for. Even if not, you could add your own specialized helpers easily. You can simply install it with

gem install sinatra-formhelpers

and use it by requiring the Gem:

require 'sinatra/form_helpers'

or, if you subclass Sinatra::Base, by additionally including the helpers:

class MyApp < Sinatra::Base
helpers Sinatra::FormHelpers
# ...
end

After all, part of Sinatra's philosophy is to be as light as possible. So if you want all the fancy things built in, Sinatra might just not be the right tool.

sinatra helper in external file

Just as you said it yourself:

Move the helpers block into another file and require it where you need.

#helpers.rb
helpers do
...
end

#project_name.rb
require 'path/to/helpers.rb'

Sinatra route function call & helpers

You've overcomplicated things. See the Helpers section of docs.

Put this in your Application controller:

helpers do
def render
@page = Page.find_by permalink: request.path_info
if @page then
else
halt 400
end
end
end

Now your route will be:

get '/*' do
render
end

Still, too complicated if you ask me, no need to ape Rails. Why not keep it simple?

require 'sinatra'

get '/*' do
@page = Page.find_by permalink: request.path_info
if @page then
haml :something
else
halt 400
end
end

That's it, that's the whole Sinatra app without recourse to inheritance and a structure that isn't required. Unless you're adding pages dynamically after the app is deployed then I'd also define the routes more explicitly.

Don't use globals. I actually can't remember the last time I saw one used, there are so many better alternatives. If you find you need one it's a clue you're going down the wrong path.

How to implement form_tag helpers without actionview?

Rails has had to use some tricks in order to get block helpers to work as wanted, and they changed moving from Rails 2 to Rails 3 (see the blogposts Simplifying Rails Block Helpers and Block Helpers in Rails 3 for more info).

The form_for helper in Rails 2.3 works by directly writing to the output buffer from the method, using the Rails concat method. In order to do something similar in Sinatra, you’ll need to find a way of writing to the output from your helper in the same way.

Erb works by creating Ruby code that builds up the output in a variable. It also allows you to set the name of this variable, by default it is _erbout (or _buf in Erubis). If you change this to be an instance variable rather than a local variable (i.e. provide a variable name that starts with @) you can access it from helpers. (Rails uses the name @output_buffer).

Sinatra uses Tilt for rendering templates, and Tilt provides an :outvar option for setting the variable name in Erb or Erubis templates.

Here’s an example of how this would work:

# set the name of the output variable
set :erb, :outvar => '@output_buffer'

helpers do
def form_helper
# use the new name to write directly to the output buffer
@output_buffer << "<form>\n"

# yield to the block (this is a simplified example, you'll want
# to yield your FormBuilder object here)
yield

# after the block has returned, write any closing text
@output_buffer << "</form>\n"
end
end

With this (fairly simple) example, an Erb template like this:

<% form_helper do %>
... call other methods here
<% end %>

results in the generated HTML:

<form>
... call other methods here
</form>

How can I use Sinatra helpers inside my model?

### helpers/my_helper.rb #################################
module MyHelper
def resolve(path)
# …helper code here…
end
module_method :resolve # Allows `MyHelper.resolve`
end
### helpers/init.rb ######################################
require_relative 'my_helper'
MyApp.helpers MyHelper # Module's methods as helpers
### models/init.rb #######################################
require_relative '../helpers/my_helper' # *vomit*
require_relative 'my_model'
### models/my_model.rb ###################################
class MyModel < Sequel::Model
def reference
MyHelper.resolve(the_path)
end
end

Sinatra helper to fake a request

The code I was using is more complex than the answer in the Sinatra README, but relied on the same mechanism. Neither my code nor the answer from the README worked under 1.2.3 due to a bug in that version. Both now work under 1.2.6.

Here's a test case of a simple helper that works:

require 'sinatra'
helpers do
def local_get(url)
call(env.merge("PATH_INFO" => url)).last.join
end
end
get("/foo"){ "foo - #{local_get '/bar'}" }
get("/bar"){ "bar" }

In action:

phrogz$ curl http://localhost:4567/foo
foo - bar

NoMethodError Sinatra Modular app

When you use helpers do ... in the top level like this you are adding the methods as helpers to Sinatra::Application and not your Profile class. If you are using the Sinatra modular style exclusively make sure you only ever use require 'sinatra/base', and not require sinatra, this will prevent you from mixing up the two styles like this.

In this case you should probably create a module for your helpers instead of using helpers do ..., and then add that module with the helpers method in your Profile class.

In helpers/aws_helper.rb:

module MyHelpers # choose a better name of course

def aws_asset( path )
File.join settings.asset_host, path
end
end

In app.rb:

class Profile < Sinatra::Base

helpers MyHelpers # include your helpers here

get '/' do
erb :index
end
end


Related Topics



Leave a reply



Submit