Skip to content

Instantly share code, notes, and snippets.

@amiel
Created July 12, 2012 06:13
Show Gist options
  • Save amiel/3096204 to your computer and use it in GitHub Desktop.
Save amiel/3096204 to your computer and use it in GitHub Desktop.
A super simple inline object to render a form
class SessionsController < ApplicationController
def new
@user = OpenStruct.new.tap do |model|
def model.class
OpenStruct.new(name: 'User').tap do |klass|
klass.model_name = ActiveModel::Name.new(klass)
end
end
end
end
end
<%= simple_form_for @user, url: new_user_session_path do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.button :submit, 'Sign In' %>
<% end %>
@amiel
Copy link
Author

amiel commented Jul 17, 2012

If only calling form_for @user resulted in a call to @user.model_name instead of @user.class.model_name.

The culprit is ActiveModel::Naming.model_name_from_record_or_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment