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.
Related
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)
I'm creating a has_and_belongs_to_many relationship with Rails. Each group has many participants and each participant can be part of many groups.
The relationship seems to be set up ok as I can use check boxes to add relationships using this in my form:
<%= collection_check_boxes(:group, :participant_ids, #participants, :id, :name) %>
However, I need to use a hidden field to submit these relationships (I use AJAX to fetch them in the view) with an array of ids (e.g. [1, 3]). I've tried using a text field like this but it doesn't save the data:
<%= f.text_field :participant_ids %>
When participant_ids saves using the checkboxes and I output it on the show view it's an array of ids but I can't seem to submit it in that format to start with.
Why can't I submit the participant_ids using a text/hidden field and is there a way around this?
For reference I've set up the join table and the models look like this:
class Group < ActiveRecord::Base
has_and_belongs_to_many :participants
end
class Participant < ActiveRecord::Base
has_and_belongs_to_many :groups
end
I also modified the group controller to work with strong parameters like this:
def group_params
params.require(:group).permit(:user_id, :purpose, :participant_ids => [])
end
I can post more code if necessary.
This answer worked for me. You will have to
<% #participants.each do |participant| %>
<% f.hidden_field 'participant_ids][', :value => participant.id %>
<% end %>
I'm adding a new model to my equasion and I'm wondering if there is a way to associate two models into one model then display any/all results within a view. For example, here is what I've currently have;
#tweet_category.order("position").each do |tweet|
<%= tweet.title %>
end
just a short example... now what if I added facebook into this. I was first thinking of creating a model thats named stuff then associate it to tweet_category and facebook_category like so;
class Stuff < ActiveRecord::Base
attr_accessible :title
belongs_to :user
has_many :tweet_category
has_many :facebook_category
end
Now in my controller I'm guessing I would do the following;
class StuffController < ApplicationController
def index
#stuff_list = Stuff.find(:all)
end
end
and in my view I would just simply do the following from above view;
#stuff_list.order("position").each do |stuff|
<%= stuff.title %>
end
am I understanding the logic here??? would that work having two models / two tables db.. etc..
First of all, I don't understand why you would need that "stuff" model. It belongs to users and has_many tweet_category and facebook_category, and just does nothing but offering a "title", when your User model could do the job ( I mean, each user could have many tweets and fb category, instead of having one or several "stuff" which has/have many of them ).
Anyway, if you want to make links between your models and then display everything in a view, first in your User model you just have to do :
class User < ActiveRecord::Base
...
has_many :facebook_categories #( I don't know how rails would pluralize it, btw, I'm just making an assumption )
has_many :tweeter_categories
end
and
class Facebook_category
...
belongs_to :user
end
and do the same fot the tweeter category
Then in your controller :
def show_everything #Here it's a custom action, but you can call it wherever you want
#users = User.all
end
And finally in your view :
<% #users.each do |user| %>
<% user.facebook_categories.all.each do |fb_c| %>
<%= fb_c.title %>
<% end %>
<% user.tweeter_categories.all.each do |t_c| %>
<%= t_c.title %>
<% end %>
<% end %>
Maybe just try to grab a better name for your models, so the pluralization doesn't get messy ( and I saw that the ".all" method is deprecated, so maybe replace it with something
Hope it helps !
Edit :
Basically, when you're doing
#users = User.all
What rails' doing is putting every hash defining every "User" in an array. So, if you want to mix two tables' arrays inside a single array, you can do something like this :
#categories = [] << Facebook_category.all, Tweeter_category.all
You will then have an array ( #category ), filled with 2 arrays ( one ActiveRecord relation for Facebook_category and one for Tweeter_category ). Themselves filled with hashes of their model. So, what you need to do is :
#categories.flatten!
Here's the API for what flatten does ( basically removing all your nested arrays inside your first tarray )
Now, you got a single array of hashes, being the informations from both your model's instances. And, if these informations can be ordered, in your view, you just have to :
<% #categories.order("updated_at").each do |i| %>
<%= i.title %>
<% end %>
I think I am on the right path for the following, though i cannot save the form data to the model. I have 2 models
class Prediction < ActiveRecord::Base
attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes
has_many :fixtures
accepts_nested_attributes_for :fixtures
end
class Fixture < ActiveRecord::Base
attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time
belongs_to :predictions
end
To create a new prediction record i have a form that takes all the fixtures and pre populates the form and the user will just add scores next to each team
<%= form_for #prediction do |f| %>
<!-- Gets all fixtures -->
<%= f.fields_for :fixtures, #fixtures<!-- grabs as a collection --> do |ff| %>
<%= ff.text_field :home_team %> VS <%= ff.text_field :away_team %><%= f.text_field :home_score %><%= f.text_field :away_score %><br>
<% end %>
<%= f.submit 'Submit Predictions' %>
<% end %>
Then i have my controller to take care of the new/create action, which i think is where i may be falling over
class PredictionsController < ApplicationController
def new
#prediction = Prediction.new
#prediction.fixtures.build
#fixtures = Fixture.all
end
def create
#prediction = Prediction.new(params[:prediction])
#prediction.save
if #prediction.save
redirect_to root_path, :notice => 'Predictions Submitted Successfully'
else
render 'new'
end
end
end
and finally my routes
resources :predictions
resources :fixtures
So when i submit the form i get the error
ActiveRecord::RecordNotFound in PredictionsController#create
Couldn't find Fixture with ID=84 for Prediction with ID=
Looking at the params being parsed (snapshot below), something does not look right, for one the home_score and away_score are not being passed through.
{"utf8"=>"✓",
"authenticity_token"=>"DfeEWlTde7deg48/2gji7zSHJ19MOGcMTxEsQEKdVsQ=",
"prediction"=>{"fixtures_attributes"=>{"0"=>{"home_team"=>"Arsenal",
"away_team"=>"Norwich",
"id"=>"84"},
"1"=>{"home_team"=>"Aston Villa",
"away_team"=>"Fulham",
"id"=>"85"},
"2"=>{"home_team"=>"Everton",
"away_team"=>"QPR",
"id"=>"86"}
Current Output of form
Any advice appreciated
Thanks
When you set #fixtures to Fixture.all in the new method in your prediction controller, you are including every fixture, not just the fixtures belonging to your prediction. When the results of the form are passed to the create controller there are fixtures associated with other predictions being passed in which is the source of the error you have reported. Perhaps you want something like #fixtures = #prediction.fixtures.
What you are doing in the fields_for block also looks fairly wrong to my eyes. You are using f.text_field for your home_score and away_score inputs. This will repeat the same form element for the prediction model in each fixture field. You won't get the result you want from this. To be honest, I'm struggling to understand how this association makes sense. Are you able to explain it a little better? My suspicion is that your models are not quite set up the way you need them to be.
edit:
OK, I think I have a better idea of what you're trying to achieve now. You're trying to create many predictions in one form and prefill the home_team and away_team from a list of existing fixtures, right?
Okay, assuming that is so, you're definitely approaching it the wrong way. You don't need a many-to-one relationship between your prediction and fixture models (as you have correctly surmised). What you need to do is generate your form by iterating over the collection of fixtures and populating the home_team and away_team fields from the current fixture instance. Do you actually need these to be editable text fields, or are you just putting them in a text field so they get passed through? If so, you could use hidden fields instead.
The problem now though, is that Rails doesn't easily allow creating multiple records in the one form. It's not something I've done before and it would take me quite a bit of trial-and-error to make it work, but here's a best guess for one way of making it so.
models
class Prediction < ActiveRecord::Base
attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes
end
class Fixture < ActiveRecord::Base
attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time
end
controller
class PredictionsController < ApplicationController
def new
#prediction = Prediction.new
#fixtures = Fixture.all
end
def create
begin
params[:predictions].each do |prediction|
Prediction.new(prediction).save!
end
redirect_to root_path, :notice => 'Predictions Submitted Successfully'
rescue
render 'new'
end
end
end
view
<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
<% #fixtures.each do |fixture| %>
<%= fixture.home_team %> vs <%= fixture.away_team %>
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
<%= text_field_tag "predictions[][home_score]" %>
<%= text_field_tag "predictions[][away_score]" %><br />
<% end %>
<%= submit_tag "Submit predictions" %>
<% end %>
So essentially I'm creating a form that returns an array of parameters. I can't use the model form helpers in this situation.
One way around this would be to create a dummy model that has_many predictions and create a nested form using this.
Anyway, that's a lot of untested code that may never get looked at so I'm going to leave it there for now.
I haven't spent a lot of time looking because my wife is making me leave soon. But if I understand things right each of your rows in your view is from fixtures and predictions model association. The params hash you are getting is a mess because default behavior is going to be a view that is updating a single record ID. So you are bringing multiple records into a single view and trying to update multiple records at once.
Your create method in your model
#prediction = Prediction.new(params[:prediction])
But your params hash being passed is this:
{"utf8"=>"✓",
"authenticity_token"=>"DfeEWlTde7deg48/2gji7zSHJ19MOGcMTxEsQEKdVsQ=",
"prediction"=>{"fixtures_attributes"=>{"0"=>{"home_team"=>"Arsenal",
"away_team"=>"Norwich",
"id"=>"84"},
"1"=>{"home_team"=>"Aston Villa",
"away_team"=>"Fulham",
"id"=>"85"},
"2"=>{"home_team"=>"Everton",
"away_team"=>"QPR",
"id"=>"86"}
So you have to get more logic in your create method in your predictions controller to iterate through the hash you are receiving and save each row one at a time.
When I use form_for :model the data is saved when I submit the form.
However when I use form_tag, the data is lost after the form is processed.
I need to use form_tag because I have two models in one form.
Is there a way to save form data with form_tag?
You are making two incorrect assumptions in your question. First, form_tag is not necessary or even recommended for multiple-model forms; Second, form_tag doesn't do anything fundamentally different from form_for, you are most likely not formatting the field names correctly for your controller.
In order to create a form with nested models, you need to use the fields_for helper in conjunction with form_for. The relationship needs to be defined first in the model with accepts_nested_attributes_for. Since you have not given us any information about your models, I will give you a made-up example:
class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :person
end
This tells ActiveRecord that the Person model can accept attributes for Address, and will pass along the attributes to the correct model to be created.
<% form_for :person do |p| %>
<% p.fields_for :address do |a| %>
use the a form builder to create
fields for the address model here
<% end %>
<% end %>
chaining the fields_for helper from the p form builder lets the helpers generate attributes in the correct format.
More information: Nested Model Forms
Pretty much the same way as before except you'll need to build the params. You can look at your log to see how params are being sent.
eg.
def create
#silly_hat = SillyHat.new( :name => params[:name], :size => params[:size], :colour => params[:colour] )
if #silly_hat.save
...