Rails form errors

I’ve been away from Rails for a long time but just recently got back to one of my old project; The Daily Deal App.

Part of this is of course creating a bunch of forms and as we all know, with forms come error messages.

What you often would do is something like this:

...

- and then have the same code for error messages in the next form and the next. But you can easily reuse these form error handlers and since you usually handle error messages the same way throughout your application using a shared file makes perfectly good sense.

So go back to your form template and add this little line instead:
{:class => 'form-horizontal'} do |f| %>

@order %>

and create a folder within apps/views/ called shared like this app/views/shared and in here you put

and there you have it, all you have to do now is put object %> into all your _form views and replace “object” with the current model your working on.

Heroku deployment

I’m currently setting up my first project on Heroku, this post will contain all the steps and issues I experience.

Log entry:
"ActionView::Template::Error (Could not find a JavaScript runtime."

Possible solution: “rake assets:precompile”

Heroku postgres for production sqlite for development

They actually argue against it over at Heroku.com since your development environment and your production environment should be as identical as possible. But I really didn’t want to install Postgres on my machine.

To accomplish running Postgres in production and Sqlite as your development and test database, you can do the following.

Open your Gemfile and change:

gem 'sqlite3'

to:

group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end

now you just need to remember to use bundle install –without production when you make changes to your Gemfile.

Rails options_for_select with include blank and selected option

If you want to use a select_tag and include a blank option it can be achieved using the following snippet:

<%= select_tag “district”, options_for_select(Customer.all.collect { |k| ["#{k.district} #{k.store}", k.id] }.insert(0, ["", 0]), params[:number].to_i) %>

- it also handles setting the selected option if one is passed – in this case in the query string. I have been blogging about this previously but noticed that I had forgotten to show how to pass the chosen option if one exists.

Rails Authentication system with polymorphic models

Uuuhhh… the title alone sounds funky :)

I want to use polymorphic models for my current app in order to represent the different types of users that the app is handling.

The base model is just a user and inside this model all the log in logic is stored and everything that is shared between all my different user types.

Inspired by cancan I wanted something that would be easy to maintain and easy to setup, therefor I decided to build a simple authentication system which I can then use in my app like this:

Visitor.can?(:view, '/secret/stuff')

and here the Visitor inherits from the User like so Visitor < User and the user is representing the database table in this case.

Inside my Visitor all the permissions are stored in a hash like this:

@permissions  = {
      :view => ["^/$", "deals", "deal\/[0-9]+$"]
    }

as you can see each key is the action and the value is an array of regular expressions. And then in the rails console I tested this like so:

["^/$", "deals", "deal\/[0-9]+$"].collect { |r| "/super/secret".match(r).nil? }

where the path (just a string) being tested is “/super/secret” then the above line of code will return:

[true, true, true]

the path “/super/secret” is not in the array of regular expressions representing the resources a visitor can view. But if I change the path to “/deal/42″ the returned array from the one liner would look like:

[true, true, false]

where false actually tells me that the visitor can view any path looking like “/deal/42″

Follow

Get every new post delivered to your Inbox.