How are parameters passed for nested attributes in Rails? - ruby-on-rails

I have two models, Ingredients and Foods:
class Food < ActiveRecord::Base
belongs_to :user
has_many :ingredients
accepts_nested_attributes_for :ingredients
attr_accessible :name, :price
end
class Ingredient < ActiveRecord::Base
belongs_to :user
belongs_to :food
attr_accessible :ingredient_name, :quantity_used
end
The schemas for the two models are as follows:
create_table "foods", :force => true do |t|
t.string "name"
t.integer "user_id"
t.float "price"
t.string "ingredient_name"
end
create_table "ingredients", :force => true do |t|
t.string "ingredient_name"
t.integer "user_id"
t.integer "food_id"
t.integer "quantity_used"
end
I'm creating a form for Food which also creates/updates the Ingredient table as well. I have the form working, and the submit button updates the correct attributes, but I have other attributes in each table that I want to update as well. For example, in the Food Controller, I want to do something like ingredient.user_id = current_user.id. I understand I can access things through params[:food], but how do I access individual fields which aren't being updates by the form?
Right now, my form is:
<%= form_for(#food) do |f| %>
<div class="field">
<%= f.label :Name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.number_field :price %>
</div>
<div>
<%= f.fields_for :ingredients do |builder| %>
<%= builder.label "Ingredient Used:" %>
<%= builder.text_field :ingredient_name %><br />
<%= builder.label "Quantity Used:" %>
<%= builder.text_field :quantity_used %><br />
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
How do I access this specific Food's ingredients in the Food#create action?
Thanks!

You might need to add :ingredients_attributes to attr_accessible in your Food model.
You can mess about with the params hash in the controller by iterating over the :ingredients_attributes or you can use assign_attributes.

Related

Creating Rails objects using same form

I am building an application to manage debts and am trying to create three objects using the same form.
The models are;
Debt (the amount owed)
class Debt < ActiveRecord::Base
belongs_to :user
belongs_to :creditor
belongs_to :debtor
accepts_nested_attributes_for :debtor, :creditor
end
Debtor (The person who owes)
class Debtor < ActiveRecord::Base
belongs_to :user
has_many :debts
end
Creditor (The person who is owed)
class Creditor < ActiveRecord::Base
belongs_to :user
has_many :debts
end
I am using the new debt form and would like to show fields for debtor and creditor. So when a new debt is created, so is the associated debtor and creditor.
I have viewed the documentation however, cannot get the fields for debtor or creditor to display on the form.
This is the form
<%= simple_form_for(#debt) do |f| %>
<%= f.error_notification %>
<!-- Debt fields -->
<div class="form-inputs">
<%= f.association :user %>
<%= f.input :amount %>
<%= f.input :commission %>
<%= f.input :invoice_issued %>
<%= f.input :invoice_date %>
<%= f.input :status %>
<%= f.input :details %>
</div>
<!-- Debtor Fields -->
<ul>
<%= f.fields_for :debtor do |debtor_form| %>
<li>
<%= debtor_form.association :user %>
<%= debtor_form.input :business_name %>
<%= debtor_form.input :abn %>
<%= debtor_form.input :first_name %>
<%= debtor_form.input :last_name %>
<%= debtor_form.input :email %>
<%= debtor_form.input :mobile_number %>
<%= debtor_form.input :phone_number %>
</li>
<% end %>
</ul>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Any help is appreciated.
EDIT
create_table "debts", force: :cascade do |t|
t.integer "user_id"
t.float "amount"
t.float "commission"
t.boolean "invoice_issued"
t.date "invoice_date"
t.string "status"
t.text "details"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
You need to build your debt's debtor or creditor in your controller:
#controller
def new
#debt = Debt.new
#debt.build_debtor
#bebt.build_creditor
end

rails Nested form fields are not rendering

I am trying to create a book consultation form with following fields from two models
User Model
- first_name
- last_name
- email
- contact_number
Message Model
:message_text
The desired form rendered should be presenting the user all the User Model fields along with a text area which would be :message_text for the Message Model.
The code of User Model is
class User < ActiveRecord::Base
has_many :messages
validates :first_name , presence: true
validates :last_name , presence: true
validates :email, presence: true
validates :contact_number,presence: true
accepts_nested_attributes_for :messages
end
The Code of Message Model
class Message < ActiveRecord::Base
validates :message_text, presence: true
belongs_to :user , :class_name => 'User', :foreign_key => 'id'
end
The index controller of User which renders the form looks like
def index
#users = User.all
#user = User.new
#user.messages.build
end
The view file looks like
<%= form_for(#user) do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="form-group">
<%= f.label :contact_number %>
<%= f.text_field :contact_number %>
</div>
<div class="form-group">
**the fields_for does not get rendered at all into the HTML**
<%= f.fields_for :messages do |ff| %>
<%= ff.label :message_text %>
<%= ff.text_field :message_text %>
<% end %>
</div>
<div class="btn primary-btn">
<%= f.submit 'Book Free Consultation'%>
</div>
All other fields get rendered. Can anybody please help me in spotting the mistake . Thank you in advance
your migration file should be like below
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :contact_number
end
end
end
class CreateMessages < ActiveRecord::Migration
def change
create_table(:messages) do |t|
t.string :message_text
t.references :user, index: true
end
end
end

Ruby adding to db

I have problem because when I try to add data to my database using form I get error unknown_attribute id_lekarza (the same with id_pacjenta and id_poradni) and I don't know why, please help
Form:
<div id="container">
<%= form_for(#refferal) do |f| %>
<div class="field">
<%= f.label :rodzaj_badania %><br />
<%= f.text_field :rodzaj_badania, placeholder: "Podaj jakie badanie ma byc wykonane", size: 35 %>
</div>
<div class="field">
<%= f.label :ilosc_badan %><br />
<%= f.number_field :ilosc_badan, placeholder: "Wpisz ilosc badan" %>
</div>
<div class="field">
<%= f.label :opis %><br />
<%= f.text_field :opis, placeholder: "Podaj krótki opis dla pracownika rejestracji/laboratorium", size: 50%>
</div>
<div class="field">
<%= f.label :data %><br />
<%= f.datetime_select :data, placeholder: "Podaj termin badania" %>
</div>
<div class="field">
<%= f.hidden_field :id_lekarza, :value => Doctor.find(session[:current_doctor_id]).id %>
</div>
<div class="field">
<%= f.hidden_field :id_pacjenta, :value => Patient.find(session[:current_patient_id]).id %>
</div>
<div class="field">
<%= f.hidden_field :id_poradni, :value => Clinic.find(session[:current_clinic_id]).id %>
</div>
<div class="actions">
<%= f.submit "Utwórz skierowanie" %>
</div>
<% end %>
</div>
Model:
class Refferal < ActiveRecord::Base
attr_accessible :data, :id_lekarza, :id_pacjenta, :id_poradni, :id_pracownika, :ilosc_badan, :opis, :rodzaj_badania
belongs_to :patient
belongs_to :employee
belongs_to :clinic
belongs_to :doctor
has_many :laboratoria
end
Schema:
create_table "refferals", force: true do |t|
t.string "rodzaj_badania"
t.integer "ilosc_badan"
t.string "opis"
t.datetime "data"
t.integer "id_lekarza"
t.integer "id_pacjenta"
t.integer "id_pracownika"
t.integer "id_poradni"
t.datetime "created_at"
t.datetime "updated_at"
end
It looks like your belongs_to associations might be your problem. Rails is really opinionated about what it expects you to name things. If your model belongs to a doctor (which is the case with Refferal model), Rails is expecting the attribute to be called doctor_id.
Try this:
Change the attribute names from :id_lekarza to :lekarza_id, and so forth for the others. Then change your model associations to specify the actual class those attributes belong to:
class Refferal < ActiveRecord::Base
attr_accessible :data, :id_lekarza, :id_pacjenta, :id_poradni, :id_pracownika, :ilosc_badan, :opis, :rodzaj_badania
belongs_to :pacjenta, class_name: 'Patient'
belongs_to :employee
belongs_to :poradni, class_name: 'Clinic'
belongs_to :lekarza, class_name: 'Doctor'
has_many :laboratoria
end
Also, you'll probably want to change your has_many association to the plural: has_many :laboratorias
Good luck!
I do not know clearly but my suspicion is that ID is probably reserved for references.
Try renaming the fields from;
id_lekarza, id_pacjenta , id_pracownika, id_poradni
to
lekarza, pacjenta , pracownika, poradni

nested attributes not displaying form correctly

I have a form from user
<%= form_for(#user) do |f| %>
<%= f.fields_for :businesses do |field| %>
<div class="field">
<%= field.label :address %>
<%= field.text_field :address %>
</div>
<div class="field">
<%= field.label :city %>
<%= field.text_field :city %>
</div>
<% end %>
<% end %>
It does not display my fields, but when i change businesses to business, then it shows, or if I remove the f from f.fields_for. But I don't think it properly saves into database.
my user model
class User < ActiveRecord::Base
has_many :businesses
accepts_nested_attributes_for :businesses
en
my business model
class Business < ActiveRecord::Base
attr_accessible :user_id, :address, :city
belongs_to :user
end
my bussiness migration
class CreateBusinesses < ActiveRecord::Migration
def change
create_table :businesses do |t|
t.integer :user_id
t.string :address
t.string :city
t.timestamps
end
end
end
Any suggestions as to what I'm doing wrong?
Thanks
You should build a business before it can display a form for it:
#user.businesses.build
Use that before using fields_for
Also check out this great gem for managing nested forms:
https://github.com/ryanb/nested_form

many to many with attributes

well, the problem is this. I have this tables.
item , person , person_item
then i do the relationship Many to Many in the model and in the controller of person i use person.items.build and use the fields_for in the view i have ALL but in the relationship table person_item is like this
create_table "person_items", :force => true do |t|
t.integer "person_id"
t.integer "item_id"
t.integer "num"
t.datetime "created_at"
t.datetime "updated_at"
end
i don't know how add the field cant when the fields person_id and item_id of relationship is fill
how i do the view for this?
part of view of person
<div class="field">
<%= f.label :nom %><br />
<%= f.text_field :nom %>
</div>
----Items-----
<% f.fields_for :items do |builder|%>
<div class="field">
<%= f.label :nomItem %><br />
<%= f.text_field :nomItem %>
</div>
i think is some like this but this fills 2 rows, one with num and all blank and the other without num.
<% f.fields_for :person_items do |pi|%>
<div class="field">
<%= pi.label :num %><br />
<%= pi.text_field :num %>
</div>
<%end%> #end of person_items
<%end%> #end of items

Resources