create_customize_cart_path do |f| %>" name="description" /> create_customize_cart_path do |f| %>" />
53195

edit model using selectbox nested_form gem

Question:

I have a nested form gem issue and can't figure it out for days.

When "edit" model, why my selectbox not filled with current value from database?

<strong>my "customize" view :</strong>

<%= nested_form_for @order_detail, :url => create_customize_cart_path do |f| %> # some field here <%= f.fields_for :order_customs do |builder| %> <%= render "order_customs_form", :f => builder %> <% end %> <%= f.link_to_add "Add Order Customize", :order_customs %> <%= f.submit %> <%end%>

<strong>my partial view (as nested) :</strong>

<%= f.label :pressed_position, "Position" %> <%= f.select :pressed_position, options_for_select(PRESSED_POSITION), {:include_blank => '-- Select Position --'} %> <%= f.link_to_remove "Remove Customize" %>

<strong>PRESSED POSITION as CONSTANT</strong> and data store "string" value

PRESSED_POSITION = [ ["Top", "top"], ["Center","center"], ["Bottom","bottom"], ["Right", "right"], ["Left", "left"], ["Top Left", "top left"], ["Top Center", "top center"], ["Top Right", "top right"], ["Center Left", "center left"], ["Center Center", "center center"], ["Center Right", "center right"], ["Bottom Left", "bottom left"], ["Bottom Center", "bottom center"], ["Bottom Right", "bottom right"] ]

For textfield it works (filled with current data), but if i using selectbox it doesn't

<strong>in my controller :</strong>

def customize @order_detail = OrderDetail.find_by_id(decrypting_id(params[:id])) rescue nil if @order_detail.present? if (current_user == @order_detail.order.user || temporary_user == @order_detail.order.temp_user_id) && @order_detail.order.order_status_id == 1 1.times{@order_detail.order_customs} else # else going here end else # else going here end end

Can You guys tell me what should I do to solve this problem? Realy appreciate it, thank you

Answer1:

I can't test it right now, but I would do it like this:

<strong>your view</strong> <strong>EDITED</strong>

<%= nested_form_for @order_detail, :url => create_customize_cart_path do |f| %> # some field here <% @order_detail.order_customs.each do |order_custom| %> <%= f.fields_for :order_customs, order_custom do |builder| %> <%= render "order_customs_form", :f => builder, :order_custom => order_custom %> <% end %> <% end %> <%= f.link_to_add "Add Order Customize", :order_customs %> <%= f.submit %> <%end%>

We have to pass the order_custom object to the partial

<strong>The partial</strong>

<%= f.label :pressed_position, "Position" %> <%= f.select :pressed_position, options_for_select(PRESSED_POSITION, PRESSED_POSITION.index{|element| element.last==order_custom.pressed_position), {:include_blank => '-- Select Position --'} %> <%= f.link_to_remove "Remove Customize" %>

This line

PRESSED_POSITION.index{|element| element.last==order_custom.pressed_position)

will return the index of the PRESSED_POSITION array where the last element is equal to the pressed_position attribute of the object order_custom.

I hope it helps :)

Recommend

  • Rails 4 custom helper method with form doesn't get output in view - fully
  • Rails multiple select form: no implicit conversion of String into Integer
  • Rails 3 Ajax Form Not Sending Params
  • using select2 gem with acts_as_taggable gem on rails
  • AJAX with Rails - Missing Template
  • Rails has_many :through association: save instance into join table
  • Set User Control's default event
  • jQuery ajax security
  • Azure table query partial partitionkey guid match
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Implementing “partial void” in VB
  • xcode don't localize specific strings
  • GridView breaks while scrolling
  • Blackberry - Custom EditField Cursor
  • Swift: Switch statement fallthrough behavior
  • Body moving without any force applied? (Box2d)
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Using variable in a value field in jMeter
  • Adding a button at the bottom of a table view
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Deleting and Updating values from a cusrsor adapter
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Is possible to count alias result on mysql
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Importing jscolor library in angular 2
  • Display Images one by one with next and previous functionality
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • php design question - will a Helper help here?
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do you join a server to an Active Directory (domain)?
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • UserPrincipal.Current returns apppool on IIS