开发者

Rails populate edit form for non-column attributes

I have the following form:

<% form_for(@account, :url => admin_accounts_path) do |f| %>
  <%= f.error_messages %>

    <%= render :partial => 'form', :locals => {:f => f} %>

    <h2>Account Details</h2>
    <% f.fields_for :customer do |customer_fields| %>
    <p>
        <%= customer_fields.label :company %><br />
        <%= customer_fields.text_field :company %>
    </p>
    <p>
        <%= customer_fields.label :first_name %><br />
        <%= customer_fields.text_field :first_name  %>
    </p>
    <p>
        <%= customer_fields.label :last_name %><br />
  开发者_开发技巧      <%= customer_fields.text_field :last_name  %>
    </p>
    <p>
        <%= customer_fields.label :phone %><br />
        <%= customer_fields.text_field :phone  %>
    </p>
    <% end %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

As well as

attr_accessor :customer

And I have a before_create method for the account model which does not store the customer_fields, but instead uses them to submit data to an API.. The only thing I store are in the form partial..

The problem I'm running into is that when a validation error gets thrown, the page renders the new action (expected) but none of the non-column attributes within the Account Detail form will show?

Any ideas as to how I can change this code around a bit to make this work me??

This same solution may be the help I need for the edit form, I have a getter for the data which it asks the API for, but without place a :value => "asdf" within each text box, it doesn't populate the fields either..


Okay, what you need to do is create a class to handle your customer with and without a Braintree gateway connection. First, create the class:

class Customer
  attr_accessor :company, :first_name, :last_name, :phone, :gateway

  def initialize gateway_id=nil
    begin
      @gateway = Braintree::Customer.find(gateway_id) unless gateway_id.nil?
    rescue Braintree::NotFoundError
    end
  end

  def company
    @gateway.nil? ? @company : @gateway.company
  end

  # Implement the rest of the methods this way as well. You can even use
  # meta-programming so that you don't repeat yourself.
end

You'll notice that calling Customer.new(id).company will work with and without an id or gateway, because if a gateway non-existent @company will be returned, and if a gateway is present the gateway's company will be returned.

Finally, hook this into your model:

class Account
  def customer
    @customer ||= Customer.new(self.gateway_customer_id)
  end

  def customer= h
    @customer = Customer.new
    @customer.company = h[:company]
    ...
    @customer
  end
end

You'll have to modify how you write code to the API so that you use customer.company instead of customer[:company] for example, but you can probably write a function within the Customer class to do this easily.

You'll have to modify your form to:

<% f.fields_for :customer, @account.customer do |customer_fields| %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜