byCoder
byCoder

Reputation: 9184

Use in form_for field, which is not in object

I have such form:

= form_for @order do |f|
  .field
    = f.label :city , "Использовать данные вашего профиля*: "
    = f.radio_button :usercase, true, :required => true
    = f.radio_button :usercase, false, :required => true
  .field
    = f.label :city , "Город*: "
    = f.text_field :city, :placeholder => "Minsk", :required => true

But field :usercase is not presented in @order. How could i send radio_button as params to my controller method? Now i get: undefined method `usercase' How to send usercase?

Upvotes: 4

Views: 1462

Answers (2)

davidb
davidb

Reputation: 8954

In your Order model you can add the usercase= method to deal witrh the data. Like this.

def usercase=(data)
  # DO SOMETHING WITH data
end

the variable data will contain the value from the usercase field.

// The attr_accessor will define usercase and usercase= but you whould have to redefine the usercase= anyway because you need to do something with the data.

Upvotes: 3

dimuch
dimuch

Reputation: 12818

Try to define usercase as virtual attribute in Order model

class Order < ActiveRecord::Base
  attr_accessor :usercase
  ...
end

Check the cast for more info.

Upvotes: 1

Related Questions