Display several nested attributes in Rails form - ruby-on-rails

I've been looking for a solution for a few days, in a Rails 4.1 app, so here is my question :
In a Rails app, I have my model User and Adress.
class User < ActiveRecord::Base
has_many :adresses
accepts_nested_attributes_for :adresses
class Adress < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
In my form, I make a form_tag for User, no problem.
But, how I can display to the final user, in a form, 2 adresses fields?
I use <%= f.fields_for :adress %> to display one, it's ok. But if I display two forms (so the user can enter 2 adresses) they have both the same name and the request post only keep one.
I read the doc at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
but, still, I don't get it.
Is there a proper way to do it?
Thanks

I would suggest you to prepare two addresses in new action, add them to the use and then in the form reneder it with foreach.
I found this kind of solution here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
Since you have multiple addresses I think foreach is way to go.

So, to help anyone who is noob in Rails and stuck the same way I was :
In your controller :
#user = User.new
#user.adresses = Adress.new, Adress.new
In your view, form :
<%= form_for #user do |f| %>
<%= f.fields_for :adresses do |a| %>
<%= wp.text_field :name %>
<% end %>
<% end %>
will print the name field for adress two times.
(thanks again to #NickCatib)

Related

Create and update with checkboxes and simple form

I am trying to do something basic but for some reason that's not working and not returning me any errors.
I have a User model and a ManualBill model.
The Manual Bill belongs to a User (I can select who paid it when I create it and it belongs to this User).
But then, once the Manual Bill is created, I also want to be able to add "manual_bills_payers", who are the ones who did not pay the bill but have to refund it.
In the end, that makes my models look like this:
class ManualBill < ApplicationRecord
belongs_to :flat
belongs_to :user
has_many :manual_bill_payers
has_many :users, through: :manual_bill_payers
end
And
class ManualBillPayer < ApplicationRecord
belongs_to :manual_bill
belongs_to :user
end
I've created an update form for the ManualBill, in which I am going to select several users (who are going to be manual_bill_payers) with checkboxes (I am in a loop which is why I called the manual_bill "bill" here.
<%= simple_form_for [#flat, bill] do |f| %>
<%= f.error_notification %>
<%= f.association :manual_bill_payers, as: :check_boxes,
:label_method =>
lambda { |owner| "#{owner.email}" },collection: User.all,
input_html: {class: "input-
bar-settings"} %>
<%= f.submit 'Add payers', class: "btn-form" %>
<% end %>
I am really confused of what to do here.
What I want to do is basically CREATE several new manual_bill_payers when updating the manual_bill by selecting users who are in my flat (all the Users of the db in the exemple above). I also want to be able to remove them by unchecking the boxes.
I've basically tried everything... from changing the models to put "has_and_belongs_to_many" instead of the "through" association, to changing the :manual_bill_payers into :users in the simple_form.
Most of the time the update goes through (no errors and redirect me to the show page) but I still get an empty array when I do ManualBillPayer.all or when I check my manual_bill.manual_bill_payers.
My update method here just in case
def update
#manual_bill = ManualBill.find(params[:id])
authorize #manual_bill
if #manual_bill.update(manual_bill_params)
redirect_to flatshare_path(#manual_bill.flat)
else
redirect_to root_path
end
end
def manual_bill_params
params.require(:manual_bill).permit(:manual_bill_payer_id)
end
I don't have any create method in this controller because the manual_bill is created somewhere else, which is why the last method is pretty empty
Where am I wrong? Is there something I am completely minsunderstanding?
I've put a raise in the update to check the params
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"280izDKna4QWfr0OhZJW6u/
UWcqlxo56yR17fBDX4QFKhNG94mYqbBLPuq+ifo3mNIV10GFk1RK7Hr5AnmosOA==",
"manual_bill"=>{"manual_bill_payer_ids"=>["", "1", "35", "34"]},
"commit"=>"Add payers",
"flatshare_id"=>"15",
"id"=>"15"}
Thanks a lot

Edit/create nested resources in Formtastic (Rails)

This question is regarding Rails 4/postgresql and the app is hosted on Heroku.
I am making a Quiz-functionality on a website and I am wondering on how to implement the forms (using Formtastic) best to make this is easy as possible. I have three models:
Quiz (has_many :quiz_questions), e.g. "Test to see how awesome you are"
QuizQuestion(belongs_to :quiz, has_many :quiz_options). e.g. "1. Which is your favorite color")
QuizOption (belongs_to :quiz_question). e.g. "Blue"
I have set up the forms like this:
<%= semantic_form_for([:admin, #quiz], :url => admin_quiz_path(#quiz.id)) do |f| %>
<%= render 'form' , :f => f %>
<% end %>
where the form looks like this:
<%= f.inputs %>
<h3>Quiz questions</h3>
<%= f.semantic_fields_for :quiz_questions do |qq_f| %>
<%= qq_f.inputs %>
<h4>Quiz options</h4>
<%= qq_f.semantic_fields_for :quiz_options do |qqo_f| %>
<%= qqo_f.inputs %>
<% end %>
<% end %>
<%= f.actions do %>
<%= f.action :submit %>
or go <%= link_to 'back', admin_quizzes_path %>
<% end %>
It seems, however, not to be working the way I want. I expect to be able to see the fields of QuizQuestion and QuizOptions in this form (there are objects for those) but I don't.
More importantly is that I would like to be able to create a New QuizQuestion and subsequently QuizOption in this form. It doesn't necessarily have to be jQuery/ajax or anything but I would like to do it all from this form.
Basically, I would like my workflow to be like:
Create a Quiz and add values to it. Click Create.
Add QuizQuestion number one and add the values to it (like "name label"). Click Create.
Add QuizOption related to QuizQuestion number one, and its "name label". Click create.
Repeat for QuizQuestion/QuizOption until the Quiz is done.
How can I do this?
For your workflow you might have to add accept_nested_attributes_for for the nested resources, this way when creating an object object you can actually create nested children (as long as they fulfill all the validations). This way:
# A quiz :has_many :quiz_questions
#quiz = Quiz.create(...)
with a declaration like:
has_many :quiz_questions
accepts_nested_attributes_for :quiz_questions
in your Quiz model you'll actually be able to create QuizQuestion from the quiz model like:
# using the previously quiz model
quiz.quiz_questions.create(...)
Doing the same for the deeply nested associations will do have the same effect.
Perhaps the reason why you don't see any field on the form is because there is not nested object created. Let me explain. When you create a new Quiz object, in your quizs_controller (or whatever the inflection for quiz is...) you need a:
def new
quiz = Quiz.new()
end
and
def create
Quiz.new(quiz_params)
end
private
def quiz_params
# whitelisted parameters sent along with the form
params.require(:quiz).permit(...)
end
if you actually want to be able to see the fields in the form you'll have to use the build method and actually populate that new object with respective the nested resources.
Note that for this to work with the form you will have to whitelist in the quizzes_controller the right attributes. You can debug the params you receive once you send the new quiz formulary and check that everything is right.
TIP! if you don't want to worry about the JS when adding nested resources dynamically, I recommend you using the cocoon gem

simple_form rails 4, set automatic association

is a little project and I try to associate patient model with consultations. one patient has_many :consultations, in my form I have:
<%= f.association :patient %>
I pass the id parameter from the patient to the action 'new' in this way:
<%= link_to new_consultum_path(:id => #patient.id) %>
And in the view I have:
How can I make that the f.association field take the correspondent patient_id automatically?
How can I be sure that the patient_id is the current patient?
If I want to hide this field is that ok if I put
instead of
Is a better way to do this?
And why in the view shows me # patient:0x007f4e7c32cbd0 ?
thanks for your help.
And why in the view shows me # patient:0x007f4e7c32cbd0
This is a Patient object.
It means you need to call an attribute of this object - EG #patient.name.
--
f.association field take the correspondent patient_id automatically
This might help:
It looks like Organization model doesn't have any of these fields: [
:to_label, :name, :title, :to_s ] so SimpleForm can't detect a default
label and value methods for collection. I think you should pass it
manually.
#app/models/patient.rb
class Patient < ActiveRecord::Base
def to_label
"#{name}"
end
end
Apparently, you need to have either title, name or to_label methods in your model in order for f.association to populate the data.
-
How can I be sure that the patient_id is the current patient?
If you're having to verify this, it suggests inconsistencies with your code's structure. If you need the patient_id to be set as the current patient, surely you could set it in the controller:
#app/controllers/consultations_controller.rb
class ConultationsController < ApplicationController
def create
#consultation = Constultation.new
#consultation.patient = current_patient
#consultation.save
end
end
I can provide more context if required.
You want to associate consultations to patients using fields_for, which is similar to form_for, but does not build the form tags.
It you start with your patient object, you can iterate through the consultation associations binding it to form fields as you go.
it would look something like this
<%= form_for #patient do |patient_form| %>
<% patient_form.text_field :any_attribute_on_patient %>
<% #patient.consultations.each do |consultation| %>
<%= patient_form.fields_for consultation do |consultation_fields| %>
<% consultation_fields.text_field :any_attribute_on_consulatation %>
<% end %>
<% end %>
<% end %>
Sorry, the code may not be exactly right.
Check out the docs for field_for here
You will also have to set accepts_nested_attributes_for consultations on patient. When you set accepts_nested_forms_for, Rails will automatically update the associated consultations object to the patient and save any edits you have made. You DEFINITELY want to use accepts_nested_attributes_for most nested form handling of this type.

Rails creating a form to update a has-many-through instance variable

I'm looking to create a form that updates a User's has_many through relationship. To be more specific, each User has many Weekdays (the model is working perfectly) and I'd like to add a form to add extra weekdays, selectable from check boxes. For reference, the (abridged) model is:
class User < ActiveRecord::Base
has_many :user_weekdays, dependent: :destroy
has_many :weekdays, -> { uniq }, through: :user_weekdays
end
class Weekday < ActiveRecord::Base
has_many :user_weekdays
has_many :users, -> { uniq }, through: :user_weekdays
end
i.e. a 'User.find(1).weekdays' might currently return Sunday and Monday, though I'd like to have a form that provides a list of all the weekdays, with those selected pushed to the user.weekdays when it's submitted.
I'm presuming it should work something like User.find(params[:id]).weekdays << (the form entries) but don't know how to do this.
I'm pretty new to Rails, and am a little lost when it comes to any form out of the ordinary. How do I build this? I've had a hunt on SO but can't find anything definitive to apply. Any advice on how to implement this in the view and controller would be GREATLY appreciated. Thanks in advance, Steve.
Got this working, using Ryan Bates' excellent Railscast on the subject. Code as follows:
<%= hidden_field_tag "user[weekday_ids][]", nil %>
<%= Weekday.all.each do |weekday| %>
<%= check_box_tag "user[weekday_ids][]", weekday.id, #user.weekday_ids.include?(weekday.id), id: dom_id(weekday) %>
<%= label_tag dom_id(weekday), weekday.day %><br>
<% end %>
Not dissimilar to the above, though this is running perfectly for me. It wouldn't save to begin with, as I hadn't defined the extra parameters with Devise (that is managing my User model). The code to fix this was as follows:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit({ weekday_ids: [] }, { wo_type_ids: [] }, :email, :current_password,
:password_confirmation, :password)
end
end
Hope this proves useful for someone - and checkout the video tutorial, it's the perfect concise run through (and well worth subscribing for)!
Steve.
If you want to display checkbox for each weekday, then you can try this code:
<% Weekday.all.each do |weekday| %>
<%= check_box_tag "user[weekday_ids][]", weekday.id, current_user.weekday_ids.include?(weekday.id) %>
<% end %>
This "weekday_ids" contain your weekday ids that you selected, and will associate with your user model through user_weekdays when it will save.

Ruby on Rails -- Saving and updating an attribute in a join table with has many => through

To simplify things, I have 3 tables :
Person
has_many :abilities, through => :stats
Ability
has_many :people, through => :stats
Stats
belongs_to :people
belongs_to :abilities
Stats has an extra attribute called 'rating'.
What I'd like to do is make an edit person form that always lists all the abilities currently in the database, and lets me assign each one a rating.
For the life of me, I can't figure out how to do this. I managed to get it to work when creating a new user with something like this:
(from the people controller)
def new
#character = Character.new
#abilities = Ability.all
#abilities.each do |ability|
#person.stats.build(:ability_id => ability.id )
end
end
From the people form:
<% for #ability in #abilities do %>
<%= fields_for "person[stats_attributes]" do |t| %>
<div class="field">
<%= t.label #ability.name %>
<%= t.hidden_field :ability_id, :value => #ability.id, :index => nil %>
<%= t.text_field :rating, :index => nil %>
</div>
<% end %>
<% end %>
This successfully gives me a list of abilities with ratings boxes next to them, and lets me save them if i'm making a new user.
The problem is that if I then load up the edit form (using the same form partial), it doesn't bring back the ratings, and if I save, even with the exact same ratings, it creates duplicate entries in the stats table, instead of updating it.
I realize I'm a terrible programmer and I'm probably doing this the wrong way, but how do I get the edit form to recall the current ratings assigned to each ability for that user, and secondly how do i get it to update the rating instead of duplicating it if the combination of person and ability already exists?
Shouldn't that be
Character
has_many :stats
has_many :abilities, through => :stats
Ability
has_many :stats
has_many :characters, through => :stats
Stat
belongs_to :character
belongs_to :ability
?
Also, is it Person or Character? You refer variously to both. (I'm going to go with Character in my answer)
I think you've fallen foul of the "I'll try to make a simplified version of my schema in order to attempt to illustrate a problem but instead make things more complex and muddle the issue by screwing it up so it doesn't make sense" syndrome. Anyway, there's a couple of issues i can see:
1) first thing is that you're adding all the possible abilities to a character as soon as they're created. This is silly - they should start out with no abilities by default and then you create join table records (stats) for the ones they do have (by ticking checkboxes in the form).
2) A simple way to manipulate join records like this is to leverage the "ability_ids=" method that the has_many :abilities macro gives you - referred to as "collection_ids=" in the api http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has_many
In other words, if you say
#character.ability_ids = [1,12,30]
then that will make joins between that character and abilities 1, 12 and 30 and delete any other joins between that character and abilities not in the above list. Combine this with the fact that form field names ending in [] put their values into an array, and you can do the following:
#controller
def new
#character = Character.new
#abilities = Ability.all
end
#form
<% #abilities.each do |ability| %>
<div class="field">
<%= t.label #ability.name %>
<%= check_box_tag "character[ability_ids][]" %>
</div>
<% end %>
#subsequent controller (create action)
#character = Character.new(params[:character]) #totally standard code
Notice that there's no mention of stats here at all. We specify the associations we want between characters and abilities and let rails handle the joins.
Railscasts episodes 196 and 197 show how to edit several models in one form. Example shown there looks similar to what you're trying to do so it might help you out (same episodes on ascicasts: 196, 197).

Resources