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

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

Closed
spastorino opened this issue Jul 23, 2013 · 10 comments
Closed
Labels

Comments

@spastorino
Copy link
Contributor

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
Copy link
Member

@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
Copy link
Member

select box is fine too

@zzak
Copy link
Member

zzak commented Jul 23, 2013

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

# 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

@spastorino
Copy link
Contributor Author

@rafaelfranca
Copy link
Member

@zzak
Copy link
Member

zzak commented Jul 23, 2013

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

@rafaelfranca
Copy link
Member

right. They are aliases

@zzak
Copy link
Member

zzak commented Jul 23, 2013

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

zzak added a commit to zzak/rails that referenced this issue Jul 23, 2013
@gaurish
Copy link
Contributor

gaurish commented Jul 23, 2013

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

@spastorino
Copy link
Contributor Author

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
Labels
Projects
None yet
Development

No branches or pull requests

5 participants