I have 3 models. Users have multiple Portfolios and Portfolios have multiple Assets.
When a user signs in, that makes him a current_user by find_by_id. When they first register, it creates a portfolio for them. That becomes the current_portfolio.
Subsequent to this, they can create portfolios which redirects them to the list of all their portfolios. Clicking the link to each will make that portfolio the current_portfolio.
#current_portfolio = current_user.portfolios.find_by_id(params[:id])
The show view for portfolios has an assets form so that they can quickly add assets to the portfolio. This is where I get stuck. Because the assets form is on the portfolios show view, my Asset controller needs to reference current_portfolio, but this is in my Portfolio controller.
Portfolio Controller:
def show
#current_portfolio = current_user.portfolios.find_by_id(params[:id])
#asset = #current_portfolio.assets.build
end
When the form is submitted, it goes to my Assets controller Create function. Create can't simply have #current_portfolio because it's on a different controller. I also can't use find_by_id(params[:id]) because :id is not providing anything.
How do I reference the second model when I have 3 models? (Sorry I'm a newb to rails...)
EDIT Update: I thought I found the solution but realized it didn't work.
I passed <%= hidden_field_tag :portfolio_id, params[:id]%> in my form and set my Create in the Asset controller to be
def create
#asset = Asset.new(params[:asset])
#asset.save
end
but it's not getting the portfolio_id which is a required field in my model.
Full form in my view
<%= form_for(#asset) do |f| %>
<%= hidden_field_tag :portfolio_id, params[:id]%>
<%= f.text_field :asset_symbol %>
<%= f.text_field :shares %>
<%= f.text_field :cost %>
<%= f.text_field :purchase_date, :type=>'date' %>
<%= f.submit "+ Add to Portfolio" %>
<%end%>
My guess would be that the portfolio_id in the hidden field isn't coming back as part of the asset hash. Maybe it's as simple as assigning the #asset.portfolio_id = params[:portfolio_id] before the save. Check you logs to see EXACTLY what is coming back in the POST
Related
I'm trying to implement a quote saving feature in a Rails app. I have a User Model that has_many :quotes and a Quote Model that belongs_to :user. Now I have a separate Book Model that would be the source of these quotes.
Within the Book's show.html.erb file, I have a form to save quotes for the current user
<%= form_for (#new_quote) do |f| %>
<div class="field">
<%= f.hidden_field :book_id, :value => #new_comment.book_id %>
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And in the Book controller, I have
def show
#new_quote = current_user.quotes.create(book_id: params[:id])
end
The quote saves fine but the problem is, since I have this Quote creation statement in the show method, everytime I go to the show.html.erb page of my Book model, it creates a Quote with an empty body.
What can I do to solve this? I was thinking it probably would involve moving this Quote creation to the actual create method of the Quote controller but I don't know how to exactly pass the parameters through.
You could just build that quote, but not save it to the database. Then the user need to send the form to save that record. Just change your show method to:
def show
#new_quote = current_user.quotes.build(book_id: params[:id])
end
I have a User controller that possesses the two following records:
user.vehicles from VehiclesController
user.trip_distances from TripDistancesController
In the Vehicles view, I need to display and update both models (Vehicle and TripDistance)
I figured out that I need to use a nested form. However, I don't understand how to implement it since my #trip_distances record needs a loop in order to display all of its content.
First, I tried to put this in the Vehicle model, but since both are NOT linked, I wonder if this is correct:
accepts_nested_attributes_for :trip_distances, allow_destroy: true
Then, in the Vehicle view's _form file, during the edit action, I have:
<%= form_for(#vehicle) do |f| %>
I need to put my nested form inside it. Here's what I got so far:
<% #trip_distances.each do |t| %>
<%= f.fields_for t do |fields| %>
<div class="field">
<%= fields.text_field :id_contract %>
<%= fields.number_field :length %>
</div>
<% end %>
<% end -%>
My trip_distances records are correctly displayed, but the unique Submit button doesn't work anymore. I'd like to update both model while clicking on one button. What am I missing?
I was able to make it work by associating the two models. Then I use the following fields_for tag:
<%= f.fields_for :trip_distances do |builder| %>
My submit button is now working, but the :trip_distances fields are not updated, I don't know why.
I have a view with 3 forms, Schedules, Workouts and Exercises, all behaving like an edit form, each. And one submit(save) button in the all the view.
When I click on the save button. Every data changed on those forms should be updated after click.
What is the best solution for this ? Javascript updating each data separated ? How to do that ? Is there a more Rails way to do this easily ?
My difficulty is how to integrated all those models in one view, while all this is happening in the show(view) from the Student model.
If you're implementing something like a profile / edit page (where you can save all the records at once), the two ways I would look at would either be to save the forms via Ajax, or use a single submit method to handle them
Ajax
The ajax method would be the most conventional:
Every form you submit will go to the form's own update method in the backend
Each form could be handled by a single button, but it's best to split them up
#app/controllers/profile_controller.rb
def edit
#schedules = Schedule.all #-> not sure how many records you're using
#workouts = Workout.all
#exercises = Exercise.all
end
#app/views/profile/edit.html.erb
<%= form_for #schedule do |f| %>
<%= f.text_field :test %>
<% end %>
# -> other forms
<%= button_to "Save", "#", id: "save" %>
#app/assets/javascripts/application.js
$("#save").on("click", function() {
$("form").submit(); // we'll have to define the form to submit
});
Single
If you submit all the forms as one, you'll have to encase them all in a single form, as sending different errors. This could be achieved by using _, and handled in the backend by looping through the different params, saving each one individually.
I'd do this:
#app/controllers/application_controller.rb
def submit
types = %w(schedules exercises workouts)
for type in types do
type.constantize.update_attributes()
end
end
This allows you to create a form with the different data types submitted in the same action:
#app/views/profile/edit.html.erb
<%= form_tag profile_submit_path do %>
<%= fields_for #schedules do |f| %>
<%= f.text_field :title %>
<% end %>
# -> fields_for for the other objects
<% end %>
This will allow you to send the updated objects to your controller, allowing them to submit
If all of your models (Schedules, Workouts and Exercises) are associated, using fields_for should be a good option.
From the above link:
<%= form_for #person do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
<%= fields_for :permission, #person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<%= f.submit %>
<% end %>
Read the guides.
You could have some simple javascript that iterates over all form tags and submits each of them.
Alternatively, if you are going to use javascript anyways, you could follow an AJAXish auto-save approach upon changing any field.
But I think it might be cleaner if you just had one form for multiple models, using fields_for.
I'm trying to build a form which preloads content from two models (two variables being passed, being shown in the textfields) and then, not saves the data but sends the altered content (from the textfields) as two(?) variables to a mailer class.
I've managed to preload the data from one of the two models but am not sure how the form_for tag has to look like to get both models loaded as well as targeting the mailer class method instead of updating the model entity when pressing "send".
Do I need the accepts_nested_attributes_for attribute inside the model if I'm not saving anything?
I hope someone could give me an small example of the crucial parts. A thousand thanks!
You can use fields_for to include other models in same form. You can use it inside the same form_for what is present.
Checkout the example here from the api docs,
<%= form_for #person do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
<%= fields_for #person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<%= f.submit %>
<% end %>
when you submit the data from this form, you can just use that data to pass to the mailer class from controller. UserMailer.get_user_info(params[:name], params[:address]).send
Creates a scope around a specific model object like #form_for, but doesn't create the form tags themselves. This makes #fields_for suitable for specifying additional model objects in the same form.
Refer Docs here:.
fields_for(record_name, record_object = nil, options = {}, &block)
I'm newbie in Rails and I'm developing an app that needs to display a patients info in a form to be updated by a doctor if needed. As soon I can select a patient from a list from sidebar I want to display first just an empty form and when a patient is selected all the info will be displayed on it.
The only way I can guess to do it is to first display the form empty and then, once the patient is selected, catch the patient on the controller and reload the page to allow the form to get the patient info.
Does anybody has a better idea? On negative answer: how can I display an empty form?
Thanks in advance for your time.
Best regards.
Update: This is the form I'm trying to present on the page. #patient is an instance variable that once the page is displayed is not defined yet . It will be displayed once the doctor will select one patient from a list on the side.
<div class="row">
<div class="span6">
<%= render 'shared/error_messages' %>
<div>
<%= form_for(#patient) do |p| %>
<%= text_field_tag 'patient[name][]', nil, :placeholder => "name", :style =>height:12px;font-size:10px;" %>
<%= text_field_tag 'patient[name][]', nil, :placeholder => "surname", :style =>"height:12px;font-size:10px;" %>
<%= p.text_field :email, :placeholder => "email", :style => height:12px;font-size:10px;" %>
<%= p.text_field :phone, :placeholder => "phone", :style => height:12px;font-size:10px;" %>
<%= check_box_tag :active %>
<%= check_box_tag :sex, 'male' %>
<%= check_box_tag :sex, 'female' %>
<%= chack_box_tag :dao %>
<%= p.submit "Sumbit", class: "btn btn-small btn-primary pull-right", id: "boton" %>
<% end %>
</div>
</div>
</div>
An here is the problem. As #patient=nil on the controller since on is selected page crashes.
Thanks again.
What you are describing here are basic controller actions on your model:
Empty Form: #new action on Patients controller
Filled Form: #edit action on Patients controller
Edit: New Strategy to Accomplish Your Proposed UX
Component 1: Nav List of Existing Patients
Iterate over collection of patient objects, outputting a link for each patient to its edit view: link_to patient.name, edit_patient_path(patient)
This nav view will be a partial that you will include in the view templates for the PatientsController #new action and #edit action.
Component 2: Empty Form for Nonexistent/New Patient
In controller new action, instantiate a new Patient object, like so #patient = Patient.new.
Render the patient form on that #patient instance variable, as you do in the view code above.
Include the nav list of existing patients as a view partial.
Component 3: Filled Form for Existing Patient
In controller edit action, instantiate the Patient instance variable, like so #patient = Patient.find(params[:id]).
Render the patient form on that #patient instance variable, as you do in the view code above.
Include the nav list of existing patients as a view partial.
Edit: Old Points on Scaffolding
I would strongly suggest using rails generators and scaffolding to set up basic forms and view templates, at least as an educational pursuit. You will, via the command line, be able to automatically generate the proper controller actions and view templates you are describing above for #new and #edit.
Read this article: http://viget.com/extend/rails-3-generators-scaffolding
Then follow this tutorial: http://railscasts.com/episodes/216-generators-in-rails-3
As you become more comfortable with Rails development, generators and scaffolding will likely not satisfy you and needs for customization. They are, however, a great starting point.
See also: http://guides.rubyonrails.org/getting_started.html#getting-up-and-running
I think, one more option is you can Instantiate your object on your form itself:
<%= form_for(Patient.new) do |p| %>
Thanks