Gabriel Savian
Gabriel Savian

Reputation: 224

How to retrieve Model data in the Scaffold on Rails?

It's been a week and I'm stuck at the same problem and I can't find a solution that fits my problem. I generated a Scaffold called Contracts (reason:string, paf:string...). Then I generated different tables using rails g model... models like Address, Person, Personal and others... all those tables Referenced to the Contracts Scaffold. I can actually render those fields in my _form.html.erb using <%= f.text_field :city %> (City is a string located in the Address model). Ok everything seems to work normally. But when I go to show.html.erb and do <p><%[email protected]%></p> I get the noMethod error "Undefined method address" Then I tried <p><%[email protected]%></p> and I get noMethod error "undefined method city for nil:NilClass" I guess that is something on the controller, but I tried to add on the.

def new @contract = Contract.new @address = Address.new end

I did the same on show controller

def show @contract.find(params[id])@address.find(params[id] end

(I'm not sure if the syntax is correct because I did a git reset and I don't remember exactly, the objective here it's just to show you guys what I tried. I know that is wrong) But not success. I saw people generating various Scaffolds for multiple tables, but rails community says that is not a good practice.

Or maybe there is an easy way to generate different tables related to my scaffold?

I did the same project with all data in the same table but my boss asked to put in different tables.

  create_table "addresses", force: :cascade do |t|
t.string "estado"
t.string "cidade"
t.string "bairro"
t.string "endereco"
t.string "cep"
t.bigint "contrato_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["contrato_id"], name: "index_addresses_on_contrato_id"

  create_table "contatos", force: :cascade do |t|
t.string "email"
t.string "phone"
t.bigint "contrato_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["contrato_id"], name: "index_contatos_on_contrato_id"


  create_table "contratos", force: :cascade do |t|
t.string "razao"
t.string "cpnj"
t.string "insc_estadual"
t.string "insc_municipal"
t.string "paf"
t.string "empresa"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false

  add_foreign_key "addresses", "contratos"
  add_foreign_key "contatos", "contratos"
  add_foreign_key "data", "contratos"
  add_foreign_key "responsavels", "contratos"

Contrato model

class Contrato < ApplicationRecord
   before_save do
    self.paf.gsub!(/[\[\]\"]/, "") if attribute_present?("paf")
   end
end

Upvotes: 0

Views: 69

Answers (2)

Chakreshwar Sharma
Chakreshwar Sharma

Reputation: 2610

You have not added association in Contrato model. If you want to get address from contrato like @contrato.address.city, you should add the assoication like below

class Contrato < ApplicationRecord
   has_one :address
   before_save do
    self.paf.gsub!(/[\[\]\"]/, "") if attribute_present?("paf")
   end
end

And for view city, make a little change in the syntax like @contrato&.address&.city

Also check the entries in address table that whether it contains the contract id for which you are checking the address present or not?

But, I want to know that you have generate a scaffold like rails generate scaffold contract field1 field2..., then the generated modal should be Contact not contract .

Upvotes: 0

cesartalves
cesartalves

Reputation: 1585

You need to define the association on the Contract model:

class Contrato < ApplicationRecord
   has_one :address
   # etc..

   before_save do
    self.paf.gsub!(/[\[\]\"]/, "") if attribute_present?("paf")
   end
end

This is what actually creates the method in your model.

Check the guide for all the associations available:

https://guides.rubyonrails.org/association_basics.html#the-belongs-to-association

Upvotes: 1

Related Questions