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
Related
I'm having trouble trying to upload images, I have a control that receives nested parameters from other models (availability, personal documents, etc ...), I made the relationships between the entities and added the "has_one_attached: img_name_example" in each model that must have an image, then I made a view with a form to be able to test the post of this form, but whenever I submit in the form I re-post the following message:
{"status":"ERROR","errors":{"disponibility":["must exist"],"personal_document":["must exist"],"personal_document_legal":["must exist"],"bank_information":["must exist"]}}
Code:
Client model:
class Client < ApplicationRecord
enum user_kind: { partner: "P", service_taker: "T" }
belongs_to :disponibility
belongs_to :personal_document
belongs_to :personal_document_legal
belongs_to :bank_information
accepts_nested_attributes_for :bank_information
accepts_nested_attributes_for :disponibility
accepts_nested_attributes_for :personal_document
accepts_nested_attributes_for :personal_document_legal
end
Client controller (require and permit):
def client_params
params.require(:client).permit(
:user_kind,
disponibility_attributes: [
:days,
:period
],
personal_document_attributes: [
:rg_front,
:rg_back,
:cpf,
:cnh_front,
:cnh_back,
:bank_card_front,
:address_proof,
:profile_picture
],
personal_document_legal_attributes: [
:cnpj,
:social_contract,
:bank_card_front
],
bank_information_attributes: [
:bank,
:account_type,
:agency,
:account_number
]
)
end
Form:
<%= form_with model: #client, local: true do |form| %>
<%= form.text_field :user_kind %> <br>
<br>
<%= form.text_field :days %> <br>
<%= form.text_field :period %> <br>
<br>
<%= form.file_field :rg_front %><br>
<%= form.file_field :rg_back %><br>
<%= form.file_field :cpf %><br>
<%= form.file_field :cnh_front %><br>
<%= form.file_field :cnh_back %><br>
<%= form.file_field :bank_card_front %><br>
<%= form.file_field :address_proof %><br>
<%= form.file_field :profile_picture %><br>
<br>
<%= form.file_field :cnpj %><br>
<%= form.file_field :social_contract %><br>
<%= form.file_field :bank_card_front %><br>
<br>
<%= form.text_field :bank %> <br>
<%= form.text_field :account_type %> <br>
<%= form.text_field :agency %> <br>
<%= form.text_field :account_number %> <br>
<br>
<%= form.submit %>
<% end %>
If I check your previous question, I think this deal how you setup your relationship between
client and other tables, if you are sure that client hold only One personal_document then
you can set as follow, if you think client can hold more than one personal document then
you have to change it to has_many
class Client < ApplicationRecord
# please note personal_document in singular
has_one :personal_document, dependent: :destroy
accepts_nested_attributes_for :personal_document, allow_destroy: :true
# now you can do some like above for disponibility, personal_document_legal, bank_information
end
class PersonalDocument < ApplicationRecord
belongs_to :client
end
class ClientsController < ApplicationController
def barang_params
params.require(:client).permit(
:user_kind,
personal_document_attributes: [
:id,
:rg_front,
:rg_back,
:cpf,
:cnh_front,
:cnh_back,
:bank_card_front,
:address_proof,
:profile_picture
]
# I think this one already correct
)
end
end
please note also since Client is parent and PersonalDocument is child
the one that relate this is field in personal_documents table that hold
client_id
check your schema here how you do migration
create_table "personal_documents", id: :serial, force: :cascade do |t|
t.integer "client_id"
# .... your other fields
end
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
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
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.
I'm trying to make a form to set values to two children objects through has_one (with :class_name option) and belongs_to relation. However, when I input and submit values through the form, both children objects have the same value even if I input different values.
I have the these two models.
(two children objects above indicates "origin" and "destination" whose class names are "Place")
class Route < ActiveRecord::Base
attr_accessible :name, :destination_attributes, :origin_attributes
has_one :origin, :class_name=>"Place"
has_one :destination, :class_name=>"Place"
accepts_nested_attributes_for :origin, :destination
end
class Place < ActiveRecord::Base
attr_accessible :address, :lat, :lng, :name, :route_id
belongs_to :route, :foreign_key => "route_id"
end
And made form using partial like following.
routes/_form.html.erb
<%= form_for(#route) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :origin, place_object: #route.origin, parent_form: f} %>
<br />
<%= render :partial => "places/nested_places_form", :locals => {record_name: :destination, place_object: #route.destination, parent_form: f} %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
places/nested_places_form.html.erb
<%= parent_form.fields_for record_name, place_object do |t| %>
<%= record_name %>
<% if place_object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#place.errors.count, "error") %> prohibited this place from being saved:</h2>
<ul>
<% #place.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= t.label :name %><br />
<%= t.text_field :name %>
</div>
<div class="field">
<%= t.label :lat %><br />
<%= t.text_field :lat %>
</div>
<div class="field">
<%= t.label :lng %><br />
<%= t.text_field :lng %>
</div>
<% end %>
Like I mentioned, the attributes of origin and destination always end up the same even when I put different values in blanks and submit from form.
How can I make this work?
Somehow you'll need to differentiate between origin and destination in the database. If they both have the same class and are stored in the same table, there's nothing to tell them apart. If you don't want to change the existing relationships, you might need to use STI for this and make the origin and destination different classes:
class OriginPlace < Place
end
class DestinationPlace < Place
end
class Route < ActiveRecord::Base
...
has_one :origin, :class_name=>"OriginPlace"
has_one :destination, :class_name=>"DestinationPlace"
...
ene
This will require a type field in the places table.