开发者

Rails 2.3.5 having trouble with form_for and fields_for

开发者 https://www.devze.com 2023-04-07 11:50 出处:网络
I am trying to add addresses to a user model via a form submit but i am having trouble. A user: class User < ActiveRecord::Base

I am trying to add addresses to a user model via a form submit but i am having trouble. A user:

class User < ActiveRecord::Base 
  has_many :addresses 
end

can add an address to his account via the accounts page, which is controlled by the accounts controller.

class AccountsController < ApplicationController
   def addresses
       @user = current_user
       @addresses = @user.addresses
       default_country = Country.find Spree::Config[:default_country_id]
       @countries 开发者_运维问答= Checkout.countries.sort
       @states = default_country.states.sort
   end
 end

In the view "/accounts/addresses", i have a form that I am trying to submit:

<% form_for @user do |user_form| %>
    <% user_form.fields_for :address do |address_form| %>
            <%= address_form.text_field :firstname, :class => 'required', :value=>"" -%>
            <%= address_form.text_field :lastname, :class => 'required', :value=>"" -%>
            <%= address_form.text_field :business_name, :class => 'required', :value=>"" -%>
            <%= address_form.text_field :address1, :class => 'required', :value=>"" -%>
            <%= address_form.text_field :address2, :value=>"" %>
            <%= address_form.text_field :city, :class => 'required', :value=>"" -%>
            ...
        <%= address_form.submit %>
    <% end -%>
<% end %>

I am looking at the one-to-many fields_for code here:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

but the problem is i have multiple addresses but only want to submit one address at a time. What do i need to add to the user/address model and/or accounts controller so I can successfully create an address and add it to the user model when I submit that form? If there is an easier way to accomplish this, please let me know.


You need accepts_nested_attributes_for

class User < ActiveRecord::Base 
  has_many :addresses 
  accepts_nested_attributes_for :addresses
end

And your form could be

<% form_for @user do |user_form| %>
    <% user_form.fields_for :addresses do |address_form| %>
            <%= address_form.text_field :firstname, :class => 'required' -%>
            <%= address_form.text_field :lastname, :class => 'required' -%>
            <%= address_form.text_field :business_name, :class => 'required' -%>
            <%= address_form.text_field :address1, :class => 'required' -%>
            <%= address_form.text_field :address2 %>
            <%= address_form.text_field :city, :class => 'required' -%>
            ...
        <%= address_form.submit %>
    <% end -%>
<% end %>

This would let you edit all existing addresses, if you need to add 1 to the list, you could

@user.addresses.build in your controller action to populate an empty one.

0

精彩评论

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