Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionView 'form cannot contain nil' after rails 5 scaffold generation #24573

Closed
kallisti5 opened this issue Apr 16, 2016 · 15 comments
Closed

ActionView 'form cannot contain nil' after rails 5 scaffold generation #24573

kallisti5 opened this issue Apr 16, 2016 · 15 comments
Milestone

Comments

@kallisti5
Copy link

Steps to reproduce

rails g scaffold user email:uniq password:digest
rails g scaffold image --api name:string version:string cost:float public:bool memory:integer
rails g scaffold container --api name:string image_id:integer user_id:integer memory:integer

Expected behavior

Not error on image#new

Actual behavior

_form.html.erb where line #1 raised:

First argument in form cannot contain nil or be empty
Extracted source (around line #1):

<%= form_for(container) do |f| %>
  <% if container.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(container.errors.count, "error") %> prohibited this container from being saved:</h2>

      <ul>

Trace of template inclusion: app/views/containers/new.html.erb

System configuration

Rails version: Rails 5.0.0.beta3
Ruby version: 2.3.0p0

@kallisti5
Copy link
Author

_A better trace_

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
    1: <%= form_for(container) do |f| %>
    2:   <% if container.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(container.errors.count, "error") %> prohibited this container from being saved:</h2>

app/views/containers/_form.html.erb:1:in `_app_views_containers__form_html_erb___4094473587655993856_27833260'
app/views/containers/new.html.erb:3:in `_app_views_containers_new_html_erb__3949885752264488795_27160180'
  Rendered XXX/.gem/ruby/2.3.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (6.2ms)
  Rendered XXX/.gem/ruby/2.3.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
  Rendered XXX/.gem/ruby/2.3.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
  Rendered XXX/.gem/ruby/2.3.0/gems/actionpack-5.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (25.7ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/_markup.html.erb (0.2ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript (20.3ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
  Rendered /usr/lib/ruby/gems/2.3.0/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb (31.7ms)
Started GET "/containers/new" for ::1 at 2016-04-15 21:47:34 -0500
Processing by ContainersController#new as HTML
  Rendered containers/_form.html.erb (2.5ms)
  Rendered containers/new.html.erb within layouts/application (4.2ms)
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.0ms)

@Edouard-chin
Copy link
Member

Edouard-chin commented Apr 16, 2016

Your error comes from the fact that new method was not created in your controller when running the generator thus the @container instance variable was not assigned. (This is the template used when scaffolding with --api option)

This error is not surprising as an API controller shouldn't have the need to have a new method, a POST request to the create method can directly be made with the desired arguments to create your model.

The confusing part is that the route new_container GET /containers/new and edit_container GET /containers/:id/edit should not be created as well as the associated views (edit, new, _form)
And more globally, should we even create any view (html.erb) when scaffolding with the --api option? We basically only need the jbuilder template.

@maclover7
Copy link
Contributor

Agree with @Edouard-chin -- it sounds like your Rails application should be a normal Rails app, not a special "API" application.

Can you please provide a sample application that replicates the error you are seeing? The likely issue here is that container should be @container.

@prathamesh-sonpatki
Copy link
Member

The confusing part is that the route new_container GET /containers/new and edit_container GET /containers/:id/edit should not be created as well as the associated views (edit, new, _form)

Because the code generating routes check for config.api_only and not to the command line options.

@spastorino Do we want to support --api with generators? IMO it's a sideeffect as of now.

@prathamesh-sonpatki
Copy link
Member

Should we rely on config.generators.api_only for the generators instead of options.api? ?

@sgrif
Copy link
Contributor

sgrif commented Apr 20, 2016

Allowing --api to generators definitely seems unintentional

@prathamesh-sonpatki
Copy link
Member

r? @spastorino

@rafaelfranca rafaelfranca added this to the 5.0.0 milestone Apr 20, 2016
vipulnsward added a commit to vipulnsward/rails that referenced this issue Apr 20, 2016
…pi apps by default, to mimic behaviour from controllers.

See also rails-api/rails-api@41f949f

Fixes rails#24573
@jmbejar
Copy link
Contributor

jmbejar commented Apr 20, 2016

I think that supporting the --api/--no-api option in the generators was never the original intention, as @prathamesh-sonpatki @sgrif commented above.

I would simply remove the api (that is a class_option in a few generators) and rely directly on config.generators.api_only, so scaffold generators rely on this centralized flag.

I'll test this approach and confirm it's a valid fix.

@rafaelfranca
Copy link
Member

It is fine to have --api in the generators, the real problem is that the views are being generated when they shouldn't

@dhh dhh assigned fxn Apr 22, 2016
@fxn
Copy link
Member

fxn commented Apr 22, 2016

@dhh 👍

@kallisti5
Copy link
Author

+1. You all are correct. Looking back I get why the error happened. However it seems like views were created that really don't make sense in api mode (which leads to user confusion)

@fxn fxn assigned spastorino and unassigned fxn Apr 23, 2016
@jeremy
Copy link
Member

jeremy commented Apr 24, 2016

Looks like this isn't blocking release candidate, though we would like to address it for final release. Removing milestone.

@jeremy jeremy removed this from the 5.0.0 milestone Apr 24, 2016
@sgrif
Copy link
Contributor

sgrif commented Apr 24, 2016

@jeremy Unsure what that means? If it'd be blocking the final release, presumably it should be blocking the release candidate.

@jeremy
Copy link
Member

jeremy commented Apr 26, 2016

@sgrif Means it's desirable for 5.0.0 release, but it's a high flux area without sufficient "real app" validation, so it's reasonable to address in 5.0.1 or 5.0.x along with the rest of the issues that 5.0.0 flushes out. Added 5.0.1 milestone to communicate this "if we can make 5.0.0, great, else 5.0.1" designation.

@jeremy jeremy added this to the 5.0.1 milestone Apr 26, 2016
@spastorino spastorino removed their assignment Jan 19, 2018
@skipkayhil
Copy link
Member

Fixed in #27604

On main (note that erb is empty):

$ rails g scaffold image --api name:string version:string cost:float public:bool memory:integer
      invoke  active_record
      create    db/migrate/20231029201748_create_images.rb
      create    app/models/image.rb
      invoke    test_unit
      create      test/models/image_test.rb
      create      test/fixtures/images.yml
      invoke  resource_route
       route    resources :images
      invoke  scaffold_controller
      create    app/controllers/images_controller.rb
      invoke    erb
      invoke    resource_route
      invoke    test_unit
      create      test/controllers/images_controller_test.rb
      invoke    helper
      create      app/helpers/images_helper.rb
      invoke      test_unit

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

No branches or pull requests