many to many with attributes - ruby-on-rails

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

Related

Ruby on Rails: Saving form check boxes to Boolean in Postgres

Rails noob here, how do I get a Rails form to save a boolean checkbox to postgres? I've tried dozens of things I've found online and none of it is working. No matter what I do, the entry in the Postgres DB is "Null". Here is my
Model(schema):
ActiveRecord::Schema.define(version: 20171227200151) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "notes", force: :cascade do |t|
t.string "note_title"
t.string "note_text"
t.boolean "note_scratch", default: false, null: false
t.boolean "note_info"
t.boolean "note_reminder"
t.boolean "note_task"
t.string "note_tags"
t.date "note_remind_date"
t.time "note_remind_time"
t.integer "note_archived"
t.date "note_archived_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
view:
<%= form_with scope: :note, url: notes_path, local: true do |f| %>
<p>
<%= f.label :note_title %><br>
<%= f.text_field :note_title %>
</p>
<p>
<%= f.label :note_text %><br>
<%= f.text_area :note_text %>
</p>
<p>
<%= f.label :notes_scratch, "Scratch" %>
<%= f.check_box :notes_scratch %>
<%= f.label :notes_info, "Info" %>
<%= f.check_box :notes_info %>
<%= f.label :notes_reminder, "Reminder" %>
<%= f.check_box :notes_reminder %>
<%= f.label :notes_task, "Task" %>
<%= f.check_box :notes_task %>
</p>
<p>
<%= f.label :note_tags %><br>
<%= f.text_field :note_tags %>
</p>
<p>
<%= f.label :note_remind_date %><br>
<%= f.date_field :note_remind_date %>
</p>
<p>
<%= f.label :note_remind_time %><br>
<%= f.time_field :note_remind_time %>
</p>
<%= f.submit %>
<% end %>
Controller:
class NotesController < ApplicationController
def new
end
def create
# for testing
# render plain: params[:note].inspect
#note = Note.new(note_params)
#note.save
redirect_to #note
end
private
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
end
If I just render the form results, it will show checked boxes as 1s, but it always comes up Null in the database, even if I set the datatype to Integer (hoping it would store the 1). All of the other fields record just fine. It's only the checkboxes that don't work.
I appreciate any assistance you can lend!
-Brick
Review your note_params (and schema.rb):
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
In your view: notes_* needs to be singular as your schema is note_*.

Rails Model Association with form

I am trying to achieve this:
It has a model called Customer that has details of the customers name
(first, middle and last) and telephone numbers (mobile and landline).
It has a model called Address that is linked to Customer. This model
will have details for a customers address (a number of address lines,
a country and a post/zip code).
A customers controller that supports two pages. The first will list
all customers in the database along with their associated address.
The second will display a form for the entry of a new customer along
with their address and allow for the creation of a new customer and
address record. It should be possible to move from either page to the
other without having to enter an URL in the browser navigation bar.
A number of points of note about this are as follows...
This is my code:
Migration:
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :address_line1
t.string :address_line2
t.string :address_line3
t.string :city
t.string :county_province
t.string :zip_or_postcode
t.string :country
t.string :customer_id
t.timestamps null: false
end
create_table :customers do |t|
t.string :first_name
t.string :middle_name
t.string :last_name
t.integer :mobile_number
t.integer :landline_number
t.timestamps null: false
end
end
end
Model:
class Address < ActiveRecord::Base
end
class Customer < ActiveRecord::Base
has_one :address
end
Form:
<%= form_for([#address,#customer]) do |f| %>
<% if #customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% #customer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :middle_name %><br>
<%= f.text_field :middle_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :mobile_number %><br>
<%= f.number_field :mobile_number %>
</div>
<div class="field">
<%= f.label :landline_number %><br>
<%= f.number_field :landline_number %>
</div>
<div class="field">
<%= f.label :address_line1 %><br>
<%= f.text_field :address_line1 %>
</div>
<div class="field">
<%= f.label :address_line2 %><br>
<%= f.text_field :address_line2 %>
</div>
<div class="field">
<%= f.label :address_line3 %><br>
<%= f.text_field :address_line3 %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :county_province %><br>
<%= f.text_field :county_province %>
</div>
<div class="field">
<%= f.label :zip_or_postcode %><br>
<%= f.text_field :zip_or_postcode %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.text_field :country %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Routes:
resources :addresses do
resources :customers
end
resources :customers do
resources :addresses
end
I haven't changed anything in my both controllers.
I am getting this error: undefined method `address_line1' for #
I don't understand where i am wrong. Any help? :( please
Schema:
ActiveRecord::Schema.define(version: 20150722160725) do
create_table "addresses", force: :cascade do |t|
t.string "customer_id"
t.string "address_line1"
t.string "address_line2"
t.string "address_line3"
t.string "city"
t.string "county_province"
t.string "zip_or_postcode"
t.string "country"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "addresses", ["customer_id"], name: "index_addresses_on_customer_id"
create_table "customers", force: :cascade do |t|
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.integer "mobile_number"
t.integer "landline_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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

How to have two different text outputs w/ ruby on rails 4

Hi I'm using text forms while using pins. My code below:
_form.html.erb
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
Index.html.erb
<div class="center">
<h1><strong><%= pin.description %></strong></h1>
<h3><%= pin.description %></h3></div>
<%= link_to image_tag(pin.image.url(:medium)), pin %>
show.html.erb
<div class="panel-heading center">
<%= image_tag #pin.image.url(:medium) %>
</div>
<div class="panel-body">
<h1><strong><%= #pin.description %></strong></h1>
<h3><%= #pin.description %></h3>
I get the same text output when I create a pin and descriptions using that code. I've tried changing the :description method into something else but then I got an error saying I can't load the page. How can I solve this problem? Thank You
ActiveRecord::Schema.define(version: 20131017222026) do
create_table "pins", force: true do |t|
t.string "description"
t.string "definition"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
I'm guessing this is your problem (your question really lacks context):
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: "form-control" %>
</div>
If you try and output fields on a form, you'll have to output the fields relating to your model's schema. For example, if you just type f.text_field :my_name, you'll get a no method error
As you've shown your schema, you could put a text field for any of the attributes your record has, such as:
<div class="form-group">
<%= f.label :definition %>
<%= f.text_field :definition, class: "form-control" %>
</div>
No Method Errors
A quick note on no method errors for you
Since Ruby on Rails is Object Orientated, everything you do with it has to involve an object. The best example of this is ActiveRecord objects - they are a variable populated with table data
The reason why you see no method errors is simply because you're trying to perform an action or method on an object which does not support it
So if you have #companies, and try to perform #companies.is_my_name_richard? - it will likely come back with a no method error. The way around this is to ensure you only call the model's attributes (if you're outputting the variable), or only use functions which have been defined in the controller or model

How are parameters passed for nested attributes in 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.

Resources