Nested form for array using upsert - ruby-on-rails

Class
has_many :translators
accepts_nested_attributes_for :translators
Class Translator was created leveraging the postgresql array datatype via t.string :idiomas, array: true.
It belongs_to :user
Intent: use the declared locales as locales for which the user can create translations, using the Mobility gem in a pattern as such:
<% current_user.translator.idiomas.each do |idioma| %>
<%= Mobility.normalize_locale(idioma) %>: <%= form.text_field "title_#{Mobility.normalize_locale(idioma)}" %>
<% end %>
Setting the user.translator.idiomas
<%= form_with(url: set_translator_user_path(#user)) do |f| %>
<%= fields_for :translator do |ff| %>
<%= I18n.available_locales %>
<% I18n.available_locales.each do |locale| %>
<%= ff.checkbox :idiomas %> <%= locale %>
<% end %>
<% end %>
<% end %>
The users_controller defines user_params params.require(:user).permit({ translators: [idiomas: []] }, and would upsert given the idioma values
def set_translator
#user.translator.upsert(idiomas: params[:idiomas])
end
Right now, the array of application defined locales states [:en, :it, :sl, :de] , but the individual locale rendering is a string, not a symbol.
a) the upsert function does not run
NoMethodError (undefined method `upsert' for nil:NilClass
#user.translator.upsert(idiomas: params[:translator][:idiomas])
changing the controller action to
if #user.translator.nil?
#translator = Translator.create(user_id: #user.id, idiomas: params[:translator][:idiomas])
else
#translator = Translator.update(idiomas: params[:translator][:idiomas])
end
correctly creates and updates the record ( idiomas: ["en", "sl", "de"])
what is correct upsert syntax?

Related

Rails form with nested attributes. text_field not appearing

Issue: I have a nested fields_for text_field not appearing, I am not sure what I have been done wrong.
Goal: While creating a record, iterate through a model with preset variables, and save a file (testing with text_field) to a join table which saves both the preset variables and the forms record ID
Models:
class PrintLocation < ApplicationRecord
has_many :shop_products, through: :shop_product_print_files
has_many :shop_product_print_files
accepts_nested_attributes_for :shop_product_print_files
end
class ShopProductPrintFile < ApplicationRecord
belongs_to :shop_products
belongs_to :print_locations
end
class ShopProduct < ApplicationRecord
...
has_many :shop_product_print_files
has_many :print_locations, through: :shop_product_print_files
accepts_nested_attributes_for :print_locations
accepts_nested_attributes_for :shop_product_print_files
...
end
Form:
<%= form_for #shop_product do |f| %>
<%= f.collection_select :product_id, #products, :id, :sku %>
<% PrintLocation.all.each do |print_location| %>
<%= print_location.title %>
<%= f.fields_for :shop_product_print_files do |a| %>
<%= a.text_field :print_file %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
With this, the text_field doesn't appear but the print_location.title's do appear. There are no errors with this.
While saving the #shop_product, I want to be able to iterate through the possible print_location variables, which are defined, and then for each possible print_location, to then be able to upload a file (text_field for testing), and then save that to the ShopProductPrintFile model which has shop_product_id and print_location_id and print_file attributes.
Is there something I am misunderstanding for how to use fields_for?
Shop Product Controller:
Create:
#shop_product = ShopProduct.new(shop_product_params)
shop = Shop.find(params["shop_product"]["shop_id"])
product = Product.find(params["shop_product"]["product_id"]) #shop_product.product_id = product.id
#shop_product.shop_id = shop.id
respond_to do |format|
if #shop_product.save!
...
Update:
#shop_product = ShopProduct.find_by(store_variant_id: params["shop_product"]["store_variant_id"])
#product = Product.find(params["shop_product"]["product_id"])
Strong Params:
def shop_product_params
params.require(:shop_product).permit(:product_id, :store_product_id, :shop_id, :store_variant_id, :sync, :shop_product_print_file_attributes[:id, :print_files, :print_location_ids => [], :shop_product_ids => []], {print_location_ids: []})
end
UPDATE 2:
Update and Create Method:
#shop_product.shop_product_print_files.build
form:
<% PrintLocation.all.each do |print_location| %>
<%= print_location.title %>
<%= f.fields_for :shop_product_print_files_attributes do |a| %>
<%= a.text_field :print_file %>
<%= a.hidden_field :print_location_id, value: print_location.id %>
<%= a.hidden_field :shop_product_id, value: shop_product.id %>
<% end %>
<% end %>
params:
def shop_product_params
params.require(:shop_product).permit(:shop_product_print_files_attributes => [:ids => [], :print_files => [], :print_location_ids => [], :shop_product_ids => []])
end
error:
Shop product print files shop products must exist
Shop product print files print locations must exist
params that pass:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"u/c103465uNCjF/trYrMleqxJ8b9wyLbU/vjPK4llYtCg/ODj92q5MN24==", "shop_product"=>{"sync"=>"1", "product_id"=>"3", "shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"6", "shop_product_id"=>"42"}, "store_product_id"=>"191234345", "store_variant_id"=>"15341234273", "id"=>"42"}, "commit"=>"Sync", "id"=>"42"}
The models haven't changed.
Print file in params still blank?
UPDATE 3:
**using this form: thanks to #arieljuod **
<%= f.fields_for :shop_product_print_files do |ff| %>
<%= ff.object.print_location.title # get the print location from the association %>
<%= ff.hidden_field :print_location_id # save the print_location_id as a hidden field %>
<%= ff.file_field :print_file # file input %>
<% end %>
with this in the new and method housing the view:
#shop_product = ShopProduct.new
PrintLocation.all.each{|p| #shop_product.shop_product_print_files.build(print_location: p)}
works on create.
Issue still arises due to not knowing the ID of the ShopProduct until the page loads due to API and there is a possibility of being multiple IDs on one page.
I use:
<% if #shop_products.find_by(store_variant_id: variant.id) %>
<% shop_product = #shop_products.find_by(store_variant_id: variant.id) %>
<%= form_for shop_product do |f| %>
...
Which, variant comes from a loop defined by an API:
<% #in_store_variants.each do |variant| %>
Now when using shop_products (from when shop_product already exists from finding by the variant.id), the fields_for won't appear. Assuming this is because no records exist in relation. Only if a shop_product.shop_product_print_files exist, will they appear.
The only work around, at this time to my knowledge, is to save all print_locations but use a boolean for which are actually active, or search for which print_locations have an ID attached. But i would rather not do it that way and just save which print_locations are chosen on create (chosen by uploading a print_file).
To "fix" this issue, I:
added accepts_nested_attributes_for reject_if: proc { |attributes| attributes['print_file'].blank? } which doesn't save ShopProductPrintFile's unless the print_file field is submitted with something...
use this form (2 forms depending on if exists or not)
<% if #shop_products.find_by(store_variant_id: variant.id) %>
<%= form_for shop_product do |f| %>
<% PrintLocation.all.each{|p| shop_product.shop_product_print_files.build(print_location: p)} %>
<%= f.fields_for :shop_product_print_files do |ff| %>
<%= ff.object.print_location.title %>
<%= ff.hidden_field :print_location_id %>
<%= ff.text_field :print_file %>
<% end %>
<%= f.submit "Sync" %>
<% end %>
<% else %>
<%= form_for #shop_product do |f| %>
<% PrintLocation.all.each{|p| #shop_product.shop_product_print_files.build(print_location: p)} %>
<%= f.fields_for :shop_product_print_files do |ff| %>
<%= ff.object.print_location.title %>
<%= ff.hidden_field :print_location_id %>
<%= ff.text_field :print_file %>
<% end %>
...
The issue with 2 is i have have PrintLocation 1,2,3 associated, it will show 9 fields, the 1,2,3 ready for update, and the 6 ready for create.
is it possible to call the PrintLocation.all.each{|p| #shop_product.shop_product_print_files.build(print_location: p)} on already created ShopProducts's for where a shop_product_print_file doesn't exist in relation to the possible print location.
So for example...
Created ShopProduct with print location, 1,2,3 (out of 6 possible)
Now, shop_product_print_location where print_location exists will show for updating in the form, so thats 1,2, and 3. How can I have it so the other 3 that weren't created now show to update the ShopProduct and create new ShopProductPrintFile's? so it is possible to update the ShopProduct to have more print_locations to the shop_product_print_file model.
I have a nested fields_for text_field not appearing, I am not sure
what I have been done wrong.
You should add this line in your create action
#shop_product = ShopProduct.new(shop_product_params)
#shop_product.shop_product_print_files.build #this one
Also change shop_product_print_file_attributes to shop_product_print_files_attributes to avoid any further errors.
You have to tell rails which PrintLocation to use on each iteration since your object does not have any
<%= f.fields_for :shop_product_print_files, print_location do |a| %>
I'm not really sure if that's what you want, but the field will appear.
EDIT: so, I think you need something like this:
On the controller
#shop_product = something_to_get_the_product
PrintLocation.all.each{|p| #shop_product.shop_product_print_files.build(print_location: p)}
I prefer to do this here, I don't like that logic on the view
Now you have all the possible print location prebuilt on the shop product object
On the form
# note here the multipart option to allow files
<%= form_for #shop_product, multipart: true do |f| %>
<%= f.collection_select :product_id, #products, :id, :sku %>
<%= f.fields_for :shop_product_print_files do |ff| %>
<%= ff.object.print_location.title # get the print location from the association %>
<%= ff.hidden_field :print_location_id # save the print_location_id as a hidden field %>
<%= ff.file_field :print_file # file input %>
<% end %>
<%= f.submit %>
<% end %>

Using enum with select in a form_tag

user.rb
enum gender_type: [:male, :female]
scope :gender, -> (gender_type) { where gender_type: gender_type}
userscontroller
#users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present?
index.html.erb
<%= form_tag users_path, method: :get do %>
<%= select_tag ?????? %>
<%= submit_tag "Search", :name => "nil", :id => "submit-gender" %>
<% end %>
the goal is to end up with:
example.com/users?gender_type=0 or example.com/users?gender_type=1
To get the mappings from a enum attribute you can use the pluralised version of the the enum attribute name:
User. gender_types
=> { male: 0, female: 1 }
You can call Hash.to_a to get an array of pairs that you can pass to options_for_select. But you may want to use .map to transform the keys.
class User
self.gender_options
# or use the I18n module to humanize the keys
self.gender_types.map { |k,v| [k.capitalize, v] }
end
end
<%= select_tag 'gender', options_for_select(User.gender_options) %>
Try that kind
select_tag 'gender', "<option value=0>male</option><option value=1>female</option>".html_safe
or smth like that
select_tag 'gender', options_for_select([["male",0],["female",1]])
and you can read rails api to find the solution
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
(I assume you mistyped in filename users.rb and it is a regular user.rb model)
User.gender_types will return a hash {"male" => 0, "female" => 1}. Sounds easy!
select_tag :gender_type, options_for_select(User.gender_types)
You can simply use like this
<%= form_for(user) do |form| %>
<%= form.select :gender_types, User.gender_types.keys.to_a %>
<% end %>
But assuming you use Rails Internationalization I18n (which I recommend) You may want to translate this enum like this in your view helper (or :
# app/helpers/users_helper.rb
def genders_select
User.gender_types.map do |k,v|
[User.human_enum_name(:gender_types, k), v]
end
end
and
<%= form_for(user) do |form| %>
<%= form.select :gender_types, #genders_select %>
<% end %>

Rails 4 : Checkbox array is not updating my attribute

My starting place was this discussion: Syntax for form_for when building an array from checkboxes
I have a call to my model passing back an array of valid options. This array then makes a series of check_box_tag
<%= form_for #game, :url => wizard_path do |f| %>
<div>
<% #game.select_races.each do |a| %>
<%= f.label a %>
<%= check_box_tag 'game[races][]', a , true %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This successfully creates an array called 'races' containing the desired output. The problem is that it doesn't actually update the races attribute. So my races attribute is still nil.
I'm sure this is a painful Rails beginner question. Any help is appreciated.
UPDATE
My allowed params were:
def game_params
params.require(:game).permit(:shattered_empire, :shards_of_the_throne, :number_of_players, :rules, :strategy_cards, :players, :races)
end
Which needed to be updated to:
def game_params
params.require(:game).permit(:shattered_empire, :shards_of_the_throne, :number_of_players, :rules, :strategy_cards, :players, {:races => []})
end

Ruby On Rails - validate nested params in controller (must not be nil)

These are my params:
{"utf8"=>"✓", "authenticity_token"=>"0RYiIDDgmOk0gCDRkAgHvv+UIgp/BuU33CLThJXqOTE=",
"order"=>
{"operation_in_orders_attributes"=>
{"0"=>{"service_operation_id"=>"5"},
"1"=>{"service_operation_id"=>""},
"2"=>{"service_operation_id"=>"4"},
"3"=>{"service_operation_id"=>""},
"4"=>{"service_operation_id"=>""}},
"kontakt"=>"comment", "Car_id"=>"50"},
"commit"=>"Dodaj",
"car_id"=>"dw815gn"}
Order has many operation_in_orders
Order has many service_operations through OperationInOrder
OperationInOrder belongs to Order
OperationInOrder belongs to ServiceOperation
ServiceOperation has many operation_in_orders
ServiceOperation has many orders through OperationInOrder
My form:
<%= form_for #order, url: new_car_order_path(#car, #order), html: {class: "add_order"} do |r| %>
<%= r.label "Service", class: :add_order_label %>
<% 5.times do %>
<%= r.fields_for :operation_in_orders do |v| %>
<%= v.collection_select(:service_operation_id, ServiceOperation.all, :id, :nazwa,include_blank: true) %>
<!-- <%= v.text_field :order_id, value: #order.id, :style => "display:none" %> -->
<% end %>
<% end %>
<%= r.label "Kontakt", class: :add_order_label %>
<%= r.text_field :kontakt %>
<%= r.text_field :Car_id, value: #car.id, :style => "display:none" %>
<%= r.label " " %>
<%= r.submit "Add", class: "sub" %>
<%= link_to "Send",ordered_path(car_id: #car.id) , class: 'sub'%>
<% end %>
I have a form where I can choose five ServiceOperations at most to an order and save.
When I save, 5 new OperationInService objects/rows are made.
Is there a possibility to not create those join tables if corresponding field on form is blank?
For example:
I fill only 2 from 5 fields. I save only these two, not 5. Now I save nil values...
I have tried to validate in OperationInService model, but there was an error (rails do not recognize format in controller).
Any ideas?
Update the accepts_nested_form_for method call in Order model as below:
class Order < ActiveRecord::Base
has_many :operation_in_orders
accepts_nested_attributes_for :operation_in_orders, reject_if: proc { |attributes| attributes['service_operation_id'].blank? }
## ..
end
This way record for operation_in_orders would not be created if service_operation_id is blank.

customized form_for tag in rails

I want to make a table within a form by making a new form_tag. The following code in ApplicationHelper fails:
module ApplicationHelper
class TabularFormBuilder < ActionView::Helpers::FormBuilder
# ... code to insert <tr> tags </tr>
end
def tabular_form_for(name, object = nil, options = nil, &proc)
concat("<table>", proc.binding)
form_for(name,
object,
(options||{}).merge(:builder => TabularFormBuilder),
&proc)
concat("</table>", proc.binding)
end
end
The view I use is:
<h1>New project</h1>
<% tabular_form_for :project, :builder => ApplicationHelper::TabularFormBuilder do |f| %>
<%= f.error_messages %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<%= f.text_field :location %>
<%= f.submit 'Create' %>
<% end %>
The error I get is:
NoMethodError in Projects#new
Showing app/views/projects/new.html.erb where line #5 raised:
undefined method `errors' for {:builder=>ApplicationHelper::TabularFormBuilder}:Hash
Any ideas how to make this custom tag work?
Is this posted verbatim? Because your second block needs to be within the closing end tag for it to access the FormBuilder class right?
I found the following tutorial which might help:
http://ramblingsonrails.com/how-to-make-a-custom-form-builder-in-rails

Resources