Boss Nass
Boss Nass

Reputation: 3522

undefined method with time_select

I am trying to use a time select with locals passed to the partial, but I am getting the following error

undefined method `open_time' for #<Merchant:0x007fb41fd0aa90>

Partial Call

= form_for @merchant, :url => admin_merchant_path(@merchant) do |form|
      #account-panel.mdl-tabs__panel
        = render :partial => 'venue', :locals => {:address_fields => form}
        = render :partial => 'hours', :locals => {:hour_fields => form}

Rendered Partial

.mdl-cell.mdl-cell--12-col
  /h3.mdl-typography--display-1.teal-heading= t('.trading_hours_title')
  - (0..6).each do |i|
    li.mdl-list__item.mdl-list__item--two-line
      span.mdl-list__item-primary-content
        = Date::DAYNAMES[i]
        span.mdl-list__item-sub-title
        = hour_fields.time_select :open_time, {:minute_step => 30, :ampm => true}

Merchant Model

has_many :trading_hours, dependent: :destroy
accepts_nested_attributes_for :trading_hours

Trading Hour Model

belongs_to :merchant

Upvotes: 0

Views: 178

Answers (2)

Aditya Y
Aditya Y

Reputation: 328

You have mentioned accepts_nested_attributes_for :trading_hours If you have open_time column in your trading hours model the following code may help:

If you use simple_form in your app

= hour_fields.simple_fields_for :trading_hours do |nest_form|
  = nest_form.open_time

Upvotes: 0

Sajan
Sajan

Reputation: 1923

you can do something like this(starting from the last line of your mentioned code):

<%= hour_fields.fields_for :trading_hours do |trading_field| %>
   <%= trading_field.time_select :open_time, {:minute_step => 30, :ampm => true} %>
<% end %>

And in Merchants controller action build trading_hours if you don't have any trading_hour:

@merchant.trading_hours.build

PS: This is is ERB(I know only this :) )

Upvotes: 1

Related Questions