Search code examples
ruby-on-railsformspartialparameters

Rails forms, params and object methods


I have form with such code:

= form_for @offer, url: some_url_path do |offer|
  = render partial: 'form', locals: { offer: offer }

withing that form partial I render other partial

= render 'prices_table', offer: offer

Contents of prices_table partial looks like this:

%tr
  %td= check_box_tag(:outer_price_toggler, 1, params[:outer_price_toggler].present?, class: "price-toggler")
  %td Aussenkabine
  %td
    .form-group.price-form-group
      = offer.text_field :outer_price, class: "form-control actual-price"
  %td
    .form-group.price-form-group
      = offer.text_field :outer_price_normal, class: "form-control normal-price"

The purpose of :outer_price_toggler checkbox is to activate other two fields, but toggler is not a part of the object. Here params[:outer_price_toggler].present? is used to understand, whether user has selected this checkbox and both other fields should be enabled for the user. For adding new offer this works perfectly, but it does not work for editing, because originally I do not have any params[:outer_price_toggler] stored anywhere.

Questions:

1) can I somehow add this :outer_price_toggler to params on edit page? That is to modify original params.

2) can I get current value of :outer_price and :outer_price_normal in order to find out whether this toggler should be activated? So, can I use something like offer.outer_price.present? || offer.outer_price_normal.present? instead of params[:outer_price_toggler].present??

If I use something like this, I get undefined method 'inner_price' for #<ActionView::Helpers::FormBuilder:0x000000082b3bf0> error, but, maybe, there is some other similar way?


Solution

  • If you could also pass in @offer object as local all the way to the prices_table partial, you can do

    %tr
      %td= check_box_tag(:outer_price_toggler, 1, offer_object.outer_price.persent? || offer_object.outer_price_normal.present?, class: "price-toggler")