I'm storing in my ddbb item configurations in my Campaign model as a jsonb column :items_json. This json will store different products with their own configurations such as color, price, etc.
Is there any approach to edit the jsonb properties (values) from the form_for? I need to be able to iterate through the JSON products and edit their properties individually.
I tried with fields_for with no luck:
<%= form_with(model: [#user,#campaign], url: admin_create_campaign_path, method: :post, local: true) do |form| %>
<%= form.fields_for(:items_json) do |sf| %>
<div class="field">
<%= sf.label :type %>
<%= sf.number_field :type %>
</div>
<div class="field">
<%= sf.label :product %>
</div>
<% end %>
<% end %>
My JSON:
"[{\"type\":\"gift\",\"product\":{\"id\":1157,\"category_id\":12,\"name\":\"Bidón light 550ml\",\"color_attributes\":[\"2E3192\",\"FFFFFF\",\"FF0098\",\"F25A23\",\"1D1D1B\",\"E30713\",\"008D36\"],\"max_printing_colors\":4,\"price\":\"5.61\",\"description\":\"Bidón de aluminio de 550ml de acabado brillo. Disponible en variada gama de colores satinados, libre de BPA. Presentado en atractiva caja de diseño.\\r\\n\\r\\n550 ml\\r\\n\\r\\nAluminio\",\"client_sku\":\"1024.0\",
Related
I am trying to create a booking system in ruby on rails, I want a student to book an instructor. I have used devise and created separate models, instructor and student. When a student is logged in, they are able to pick their instructor which is listed, and press 'Book a lesson' both their IDs are transferred to the lesson new form.
I am having two problems, the button to transfer the chosen instructors ID and the student who is signed in ID, to the lessons new page, for the student to book a lesson time and date.
Instructor#index
<% #instructors.each do |instructor| %>
<%=instructor.id%> : <%= instructor.email %>
<%#= button_to 'Book a lesson', new_lesson_path(instructor_id: instructor) %>
<%= link_to 'Book a Lesson', {controller: :lesson, :action => :new, :instructor_id => #instructor.id} %>
<% end %>
Lessons Controller
# GET /lessons/new
def new
# #lesson = Lesson.new
#instructor = Instructor.find(params[:instructor_id])
#lesson = Lesson.new(student_id: Student.find(session[:student_id]).id)
#lesson.instructor_id = #instructor.id
end
lesson FORM new
<%= form_for(#lesson) do |f| %>
<div class="main-title"> New Lesson for "<%= #lesson.instructor.email %> "</div>
<%= f.hidden_field :instructor_id %>
<%= f.hidden_field :student_id %>
<div class="field">
<%= f.label :lesson_date %><br>
<%= f.date_select :lesson_date %>
</div>
<div class="field">
<%= f.label :lesson_start_time %><br>
<%= f.time_select :lesson_start_time %>
</div>
<div class="field">
<%= f.label :lesson_end_time %><br>
<%= f.time_select :lesson_end_time %>
</div>
<div class="actions">
<%= f.submit "Add a lesson" %>
</div>
<% end %>
enter image description here
I have a dashboard that shows results fetched from users and nannies model.
In my user.rb, I have
User has_many :nannies
The nanny.rb has
Nanny belongs_to :user
User has some basic attributes (name, email, phone etc)
Nanny has other attributes (gender, hours, days etc)
Right now, I have these lines to implement Ransack and fetch users according to filters. The index method of the controller is
#users = User.ransack(params[:q])
#people = #users.result
I can filter out the results based on attributes from Nanny. The filter form in the view is like this
<%= search_form_for #users do |f| %>
<%= f.label :firstname_cont %>
<%= f.search_field :firstname_cont %>
<%= f.label :lastname_cont %>
<%= f.search_field :lastname_cont %>
<%= f.label :role_eq %>
<%= f.search_field :role_eq %>
<%= f.label :nannies_gender_cont %>
<%= f.search_field :nannies_gender_cont %>
<%= f.label :nannies_hours_eq %>
<%= f.search_field :nannies_hours_eq %>
<%= f.submit %>
<% end %>
The results are displayed like this
<% #people.each do |test| %>
<%= test.id %>
<%= test.firstname %>
<%= test.email %>
<%= test.hours %>
<% end %>
I am getting an error 'undefined method hours for User' (which obviously belongs to the Nanny model).
I need to be able to achieve the following
Load all Users and its associated Nannies
Run Ransack and filter out the results
Display all attributes from all associated models in the view
Would really appreciate any guidance/correction in this issue.
Assuming your question is how to fix undefined method hours for User:
The following solution is not really a Ransack "thing", but more just a Rails "thing":
<% #people.each do |test| %>
<%= test.id %>
<%= test.firstname %>
<%= test.email %>
<% test.nannies.each do |nanny| %>
<%= nanny.hours %>
<% end %>
<% end %>
You need to loop through each nanny that belongs to test (of which is a User record), and from there only when you can access hours value for each nanny
View Output
I am trying to loop through an array of values using simple_form.
Where #resume.employerorg holds the following values:
["Kaiser Santa Clara Hospital", "Kaiser Hospital",
"UC Medical Center", "UCD Medical Center", "Some Hospital", "Sutter Auburn Faith Hospital", "Kaiser Roseville Hospital",
"Sutter Roseville Hospital"]
In my view I have the following
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i| %>
<%= f.input :employerorg, label: 'First Employer' %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
But this creates 8 input fields each filled with
Kaiser Santa Clara Hospital Kaiser Hospital UC Medical Center UCD
Medical Center Some Hospital Sutter Auburn Faith Hospital Kaiser
Roseville Hospital Sutter Roseville Hospital
How would I fill each input with one individual item from the array rather than fill each input with the whole array 8 times?
You should use the value of i to generate the input. Something like this:
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each_with_index do |i, index| %>
<% input_id = "employerorg_#{index}".to_sym %>
<%= f.input input_id, label: 'First Employer', input_html: {value: i} %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
You need to generate the input_id based on the item index. Otherwise, all the inputs will have the same name and ID and would be undistinguished.
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i| %>
<%= f.input :employerorg, label: 'First Employer' %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
Understanding what's going on here: every time you tell Ruby/Rails to .each do an object Collection, you specify a "pointer", if you will, to refer to each object within, in your case, you've specified i.
If you're using a simple array as your input, you can just use i to output the value.
As Fredrico Mentioned, you should also be utilizing the object's index to generate your ID.
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i, index| %>
<%= f.input "emp-#{index}".to_sym, label: "Employer: #{index}", input_html: {value: i} %>
<% end %>
<br/>
<% end %>
I want to translate a rails form using i18n system.
My model attributes are translated correctly, but when I want to translate submit actions, the model name is not translated.
Here is my fr.yml locale file
fr:
activerecord:
model:
character:
one: "Personnage"
other: "Personnages"
attributes:
character:
name: "Nom"
title: "Titre"
biography: "Biographie"
helpers:
submit:
update: "Mise à jour %{model}"
My _form.html.erb is
<%= form_for(#character) do |f| %>
<% if #character.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#character.errors.count, "error") %> prohibited this character from being saved:</h2>
<ul>
<% #character.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :biography %><br />
<%= f.text_area :biography, :rows => 8%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
On my form, I expected the update button to be "Mise à jour Personnage", but I still have "Mise à jour Character".
Thanks for your help !
Looks like you have a model where you should have models.
Change your fr.yml to look like this:
fr:
activerecord:
models:
character:
one: "Personnage"
other: "Personnages"
...
See the rails i18n documentation section on Translations for Active Record Models for details.
This is the default behavior of the submit button when used in the form_for tag...it will show the Update Character instead of "Update #{Model.name}"...
I'm attempting to edit a model's nested attributes, much as outline here, replicated here:
<%= form_for #person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in #person.addresses %>
<%= person_form.fields_for address, :index => address do |address_form|%>
<%= address_form.text_field :city %>
<% end %>
<% end %>
<% end %>
In my code, I have the following:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<% for subitem in #meal.meal_line_items %>
<!-- # Edit 2: I need to display information here about the subitem
Which I can't find a way to pass it to the partial, or work in
this manner for existing items
-->
<%= subitem.food.name %>
<%= subitem.food.calories %>
<%= f.fields_for subitem, :index => subitem do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
This works great, except, when I look at the HTML, it's creating the inputs that look like the following, failing to input the correct id and instead placing the memory representation(?) of the model. As a result, an update fails:
<input type="text" value="2" size="30" name="meal[meal_line_item][#<MealLineItem:0x00000005c5d618>][servings]" id="meal_meal_line_item_#<MealLineItem:0x00000005c5d618>_servings">
EDIT:
The reason I'm attempting to do it in this method is that I need to gather some information on associations for existing meal_line_items. For example, in the area where I took out code, I have some code to the effect of:
<%= subitem.food.name %>
<%= subitem.food.calories %>
Getting this information won't work if I am using a form builder with partials, at least, not in my trials.
Edit 2:*
See the edit in the code. Here's my MealLineItem
class MealLineItem < ActiveRecord::Base
# Associations ---------------------
belongs_to :food
belongs_to :meal
end
And meal accepts_nested_attributes for the model. As you can see it belongs to both food and meal model. For the existing meal_line_item I need to do something like:
meal_line_item.food.name
Is there f. missing from <%= fields_for?
--edit
Have you tried:
<%= f.fields_for 'meal[meal_line_item][]', subitem do |line_item_form| %>
--edit
Docs say that it should work without loop too:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<%= f.fields_for :meal_line_items do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<%= f.submit %>
<% end %>
Have to test this but maybe this approach?
_form
<%= form.fields_for :meal_line_items do |meal_line_item_form| %>
<% #meal.meal_line_items.each do |meal_line_item| %>
<%= render :partial => "meal_line_items/meal_line_item", :locals => { :meal_line_item_form => meal_line_item_form, :meal_line_item => meal_line_item } %>
<% end %>
<% end %>
meal_line_items/_meal_line_item.erb
<%= meal_line_item_form.label :servings %><br/>
<%= meal_line_item_form.text_field :servings %><br/>
<%= meal_line_item_form.label :food_id %><br/>
<%= meal_line_item_form.text_field :food_id %><br/>
EDIT
here's a link to an example for setting the formbuilder iterator directly (Rails 2.3.8 though). The associations between Outbreak -> Incidents -> Location should be similiar to the ones for Meal -> Meal_line_items -> Food.
AJAX update of accepts_nested_attributes_for partials
After searching high and low, I found the error. Although I was modifying the partial and was receiving a NameError it's because I was calling the partial from a helper method - exactly the same problem as stated in the following question:
rails fields_for render partial with multiple locals producing undefined variable