Rails 4.2 Sequel Model - Form_for on new not working as expected

224 views Asked by At

I created a new Rails 4.2 project, set up the Sequel Gem as per docs and ran the following command to set up my first section of the project:

rails generate scaffold Author nom_de_plume:string real_name:string email_address:string code_of_conduct_date:datetime created_at:datetime updated_at:datetime --orm=sequel

rake db:migrate

Based on reading on Stackoverflow and some Google links, my model skeleton looks like:

class Author < Sequel::Model
  # ---
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  def persisted?
    true
  end
  # ---
  plugin :validation_helpers
  plugin :after_initialize
  # ---
  def validate
    super
    validates_presence [:nom_de_plume, :real_name, :email_address, :code_of_conduct_date]
    validates_unique([:nom_de_plume, :email_address])      end # def validate
  # ---
  def after_initialize
    super
  end # def after_initialize
end # class Author < Sequel::Model

The controller is stock / unmodified as yet. When I go to http://localhost:3000/authors/ everything works as expected.

However, when I click the http://localhost:3000/authors/new "New Author" link, I get the following error:

Showing /project/app/views/authors/_form.html.erb where line #1 raised:

No route matches {:action=>"show", :controller=>"authors", :id=>nil} missing required keys: [:id]

Extracted source (around line #1):
1 <%= form_for(@author) do |f| %>
2
3 <% if @author.errors.any? %>
4 <div id="error_explanation">
5 <h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2>
6

Clearly this is broken behaviour; "show" should not be getting invoked on a "new" request.

I created a new project, did not use the sequel-rails gem & associated configuration (using the default ActiveRecord), and the exact same code works as expected without errors.

I've spent a couple of hours doing searches, and cannot find a solution for this. I've wiped the project and restarted, and can reproduce the behaviour described consistently.

I know I could just drop sequel-gem and go back to AR, but I'd really rather not do so.

I'd appreciate a point in the right direction to getting this solved. Thanks in advance.

2

There are 2 answers

0
AudioBubble On

Just include some more ActiveModel sub-modules, full list here:

class Author < Sequel::Model

  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Model
  include ActiveModel::AttributeMethods
  include ActiveModel::Dirty
  include ActiveModel::Serialization

  def persisted?
    true
  end

  # ...

end 
0
Jeremy Evans On

You should probably use the active_model Sequel plugin (shipped with Sequel) if you are using Sequel with Rails' form helpers. You can use Sequel::Model.plugin :active_model before loading your model classes to do that. If you still have problems after that, please post more details. Note that the problem appears to be a Rails issue, not a Sequel issue, as the failure is in generating the correct route.