Skip to content

Loading…

Scaffold generator's view points should point to the _id field of the reference #11573

Closed
spastorino opened this Issue · 10 comments

5 participants

@spastorino
Ruby on Rails member

I'm opening this easy to fix issue for people looking for easy things to fix :).

Steps to reproduce

rails new myapp
rails g scaffold currency name
rails g scaffold account name currency:belongs_to
rake db:migrate
rails s

And try to tie an account with a currency. The account view should be pointing to currency_id.

@prathamesh-sonpatki

@spastrino How can i know the currency_id while creating account? If there was select box of currencies, i can select one and that can have value which will be currency_id from database. But here, as it is textbox, i don't know what is the id of the currency.

@rafaelfranca
Ruby on Rails member

select box is fine too

@zzak
Ruby on Rails member

I wrote a test for this, but need feedback. Looks like Rails::Generators::NamedBase is where I want to look? Pointers? :heart: :heart: :sparkling_heart:

# railties/test/generators/scaffold_generator_test.rb
  def test_scaffold_generator_belongs_to
    run_generator ["account", "name", "currency:belongs_to"]

    assert_file "app/models/account.rb", /belongs_to :currency/

    assert_migration "db/migrate/create_accounts.rb" do |m|
      assert_method :change, m do |up|
        assert_match(/t\.string :name/, up)
        assert_match(/t\.belongs_to :currency/, up)
      end
    end

    assert_file "app/controllers/accounts_controller.rb" do |content|
      assert_instance_method :account_params, content do |m|
        assert_match(/permit\(:name, :currency_id\)/, m)
      end
    end

    assert_file "app/views/accounts/_form.html.erb" do |content|
      assert_match(/<%= f\.text_field :name %>/, content)
      assert_match(/<%= f\.text_field :currency_id %>/, content)
    end

  end
$ ruby -Itest test/generators/scaffold_generator_test.rb
Run options: --seed 61346

# Running:

.F............

Finished in 1.572028s, 8.9057 runs/s, 152.6690 assertions/s.

  1) Failure:
ScaffoldGeneratorTest#test_scaffold_generator_belongs_to [test/generators/scaffold_generator_test.rb:307]:
Expected /<%= f\.text_field :currency_id %>/ to match "<%= form_for(@account) do |f| %>\n  <% if @account.errors.any? %>\n    <div id=\"error_explanation\">\n      <h2><%= pluralize(@account.errors.count, \"error\") %> prohibited this account from being saved:</h2>\n\n      <ul>\n      <% @account.errors.full_messages.each do |msg| %>\n        <li><%= msg %></li>\n      <% end %>\n      </ul>\n    </div>\n  <% end %>\n\n  <div class=\"field\">\n    <%= f.label :name %><br>\n    <%= f.text_field :name %>\n  </div>\n  <div class=\"field\">\n    <%= f.label :currency %><br>\n    <%= f.text_field :currency %>\n  </div>\n  <div class=\"actions\">\n    <%= f.submit %>\n  </div>\n<% end %>\n".

14 runs, 240 assertions, 1 failures, 0 errors, 0 skips

https://gist.github.com/zzak/6065288

@zzak
Ruby on Rails member

@spastorino same thing applies to :references right? ie: should be :currency_id

@rafaelfranca
Ruby on Rails member

right. They are aliases

@zzak
Ruby on Rails member

@rafaelfranca Thank you, I have a patch, submitting a PR now

@zzak zzak referenced this issue
Commit has since been removed from the repository and is no longer available.
@zzak zzak added a commit to zzak/rails that referenced this issue
@zzak zzak Use GeneratedAttribute#column_name if #reference? scaffold_generator …
…in _form view [Fixes #11573]
5fab3b9
@gaurish

@spastorino
Level: Easy tag is an excellent idea. please tag more issues like this. :+1:

@spastorino
Ruby on Rails member

Thanks a lot @zzak may Rails be with you ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.