Spree override deface association form giving mismatch error - ruby-on-rails

I have to add a association form on Spree Return Authorization edit page and for that I have added code in overrides/spree/return_authorizations/_form/add_custom_tracker.html.erb.deface
<!-- insert_before 'erb[loud]:contains("f.field_container :stock_location")' -->
<%= f.field_container :easypost_tracker do %>
<%= f.fields_for #return_authorization.build_custom_tracker do |tracker| %>
<%= tracker.label :tracking_code %>
<%= tracker.text_field :tracking_code, class: 'fullwidth' %>
<%= tracker.label :carrier %>
<%= tracker.text_field :carrier, class: 'fullwidth' %>
<% end %>
<% end %>
Added association also and also permit params in spree.rb
base.has_one :custom_tracker, class_name: 'Custom::Tracker',
foreign_key: :spree_return_authorization_id
base.accepts_nested_attributes_for :custom_tracker
config/spree.rb
Spree::PermittedAttributes.return_authorization_attributes.push custom_tracker: [:id, :tracking_code, :carrier]
Form appearing on view fine but when I submit form its giving an error
ActiveRecord::AssociationTypeMismatch in Spree::Admin::ReturnAuthorizationsController#update
Easypost::Tracker(#286980) expected, got {"tracking_code"=>"5656", "carrier"=>"UPS"} which is an instance of ActiveSupport::HashWithIndifferentAccess(#295580)

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 %>

Can one simple_form create instances of multiple models?

I got simple_form for testrun model with multiple checkboxes, that save an array of testcases in a model field
app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.input :testcase, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<%= f.submit %>
<% end %>
It works fine, but from now I need to create another model called testcase. After submitting form, besides creating a new testrun instance, I need to create testcase instances which depends on every flag checked.
Any idea how can I do it?
You need to use accepts_nested_attributes_for and simple_fields_for. Assuming you have has_many :testcases in Testrun and the field name of Testcase is name, the below steps should put you in the right direction.
#app/models/testrun.rb
accepts_nested_attributes_for :testcases
#app/controllers/testrun_controller.rb
def new
#testrun = Testrun.new
#testrun.testcases.build
end
private
def testrun_params
params.require(:testrun).permit(:field1, :field2.., testcases_attrubtes: [name: []])
end
#app/views/testruns/_form.html.erb
<%= simple_form_for #testrun do |f| %>
<%= f.simple_fields_for :testcases do |testcase| %>
<%= testcase.input :name, as: :check_boxes,
collection: [["testcase1", :testcase1], ["testcase2", :testcase2], ... ]%>
<% end %>
<%= f.submit %>
<% end %>

Getting "builder", "parent_builder", and "namespace" fields returned from Postgresql/Hstore in rails

I built a simple application, which i am trying to update with a form, but got some weirdness going on.
I have a column 'field_items' which is an hStore. If i call debug on the model in my view...
<%= debug #app.field_items %>
I get the two proper items returned. In the rails console i also do not see the three extras.
I have a form_for where i iterate over the 'field_items'
In my form it returns three extra fields "builder", "parent_builder", and "namespace"
Anyone have any ideas? I have noticed if i comment out the serialize line on :field_items in the model, it doesn't return the three extra attributes
Here is my model
class App < ActiveRecord::Base
belongs_to :page
attr_accessible :content, :title, :layouts, :field_items
serialize :layouts, ActiveRecord::Coders::Hstore
serialize :field_items, ActiveRecord::Coders::Hstore
end
Here is the form/code from my edit view
<%= form_for [:admin, #app], :html => { :class => "form app_fields_form" } do |f| -%>
<div id="app_fields_row_container">
<%= f.fields_for :field_items, #app.field_items do |fi| %>
<% #app.field_items.try(:each) do |key, value| %>
<div class='app_fields_row item_row'>
<div class="column col1"><%= text_field_tag key, key, :class => "form_text_field dynamic_attr" %></div>
<div class="column col2"><%= fi.select key, options_for_select(APP_FIELD_TYPES, value), {}, {:class => "form_select"} %></div>
<div class="column col3">x</div>
</div>
<% end %>
<%- end -%>
</div>
<% end -%>
I was having the same issue. Removing #app.field_items from fields_for worked for me
<%= f.fields_for :field_items do |fi| %>
Remove the following line and it should work fine:
<%= f.fields_for :field_items, #app.field_items do |fi| %>
I had the exact same problem, and removing the fields_for call solved the issue, as there is no need for it as it isn't a nested resource.

rails 3 has_many :through Form with checkboxes

Asked similar before.
Rails 3 has_many :through Form
But can't get the relationship with employmentships to be created from the users form.
Have read http://www.justinball.com/2008/07/03/checkbox-list-in-ruby-on-rails-using-habtm/
and http://millarian.com/programming/ruby-on-rails/quick-tip-has_many-through-checkboxes/ (which I was really hoping that it worked.)
Form submits, but only creates a blank record in employmentship.
<%= form_for #user do |f| %>
...
<p>
<% Company.all.each do |company| %>
<%= check_box_tag :company_ids, company.id, #user.companies.include?(company), :name => 'user[company_ids][]' -%>
<%= label_tag :companies_ids, company.id %>
<% end %>
</p>
<p><%= f.submit %></p>
<% end %>
Include a hidden field tag in the form to make sure something gets submitted when none of the check boxes are selected. This should work, after the <%end%>:
<%= hidden_field_tag "user[company_ids][]" %>
I may be wrong, but I think that the first arg of the check_box_tag function is the actual name of the input, so instead of
check_box_tag :company_ids, company.id, #user.companies.include?(company), :name => 'user[company_ids]'
you could try something like
check_box_tag 'user[company_ids]', company.id, #user.company_ids.include?(company.id)
Let me know if it works!

Problems with nested form fields showing up

I'm attempting to implement nested object forms for my site, using Ryan Daigle's blog post as a guide (http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes). For some reason, the nested form fields don't appear in the view.
class Instruction < ActiveRecord::Base
has_many :steps
accepts_nested_attributes_for :steps
end
class Step < ActiveRecord::Base
belongs_to :instruction
end
<% form_for #instruction do |instruction_form| %>
<%= instruction_form.error_messages %>
<p>
<%= instruction_form.label :title %><br />
<%= instruction_form.text_field :title %>
</p>
<p>
<%= instruction_form.label :difficulty %><br />
<%= instruction_form.text_field :difficulty %>
</p>
<% instruction_form.fields_for :steps do |step_form| %>
<%= step_form.label :explanation, 'Explanation: ' %>
<%= step_form.text_field :explanation %>
<% end %>
<p><%= instruction_form.submit "Submit" %></p>
<% end %>
When I change instruction_form.fields_for :steps do |step_form| to instruction_form.fields_for :step do |step_form|, the form renders but upon submission, I get an 'unknown attribute: step' error.
What I'm doing seems to match the tutorial. What should I check? Thanks.
What is going on in your controller? I haven't read the tutorial yet, and can't seem to pull it up right now (down?) but are you building out an object in memory to fill out?
in your controller, in your "new" action, make sure that you are
#instruction = Instruction.new
#instruction.steps.build
This will instantiate a Step in memory as a "placeholder" for your form to fill in . . . at least this is what I do in my own controller when using a accepts_nested_attributes_for, and it works great.
Let me know if it works, and once I can pull up the tutorial I may have to edit this

Resources