form fields not related to the model

I have an existing form which is tied to a model named 'Order', but i want to add new form fields that will capture Credit Card info such as name, cc number, etc to be processed on a 3rd party payment gateway.

But since i don't want to save CC info in our database, there are no corresponding columns of that in my order table. And this gives me an error when submitting the form that those Credit card input fields are not 'part' of the order model.


You can use attr_accessor

 class Order < ActiveRecord::Base

   attr_accessor :card_number


 end

Now you can do Order.first.card_number = '54421542122' or use it in your form or whatever else you need to do.

See here for ruby docs http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-attr_accessor and here for a useful stackoverflow question What is attr_accessor in Ruby?

Don't get it mixed up with attr_accessible! Difference between attr_accessor and attr_accessible


If I understand your answer correctly, what you want to do is explained in the official wiki page here: Create a fake input that does NOT read attributes. You can use a field not related to any real database column by Edward's suggestion, however you don't need to define an attribute in your model if the form field is nothing to do with the model.

In summary, the trick explained in the page is defining a custom input called 'FakeInput' and use it like this:

<%= simple_form_for @user do |f| %>
  <%= f.input :agreement, as: :fake %>
  ....

Do not forget to restart your rails server after adding/modifying a custom input as Fitter Man commented.

UPDATE: Please note that the official wiki page has updated and the sample code on the wiki page is not working for those which use older versions of SimpleForm. Use code below instead if you encounter an error like undefined method merge_wrapper_options for... . I'm using 3.0.1 and this code works well.

class FakeInput < SimpleForm::Inputs::StringInput
  # This method only create a basic input without reading any value from object
  def input
    template.text_field_tag(attribute_name, input_options.delete(:value), input_html_options)
  end
end

The best way to handle this is to use simple_fields_for like so:

<%= simple_form_for @user do |f| %>
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :email %>

  <%= simple_fields_for :other do |o| %>
    <%= o.input :change_password, as: :boolean, label: 'I want to change my password' %>
  <% end %>
<% end %>

In this example, I have added a new field called change_password which is not part of the underlying user model.

The reason this is a good approach, is that it lets you use any of the simple form inputs / wrappers as fields. I don't care for the answer by @baxang, because it doesn't allow you to use different types of inputs. This seems more flexible.

Notice though for this to work, I had to pass :other to simple_fields_for . You can pass any string/symbol as long as there is not a model with that same name .

Ie unfortunately I can't pass :user , as simple_form would try to instantiate a User model, and we'd get the same error message again...

链接地址: http://www.djcxy.com/p/25778.html

上一篇: @instance之间的区别

下一篇: 形成与模型无关的字段