Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';t mass在使用designe时指定关联模型的受保护属性_Ruby On Rails_Ruby_Ruby On Rails 3_Devise - Fatal编程技术网

Ruby on rails Can';t mass在使用designe时指定关联模型的受保护属性

Ruby on rails Can';t mass在使用designe时指定关联模型的受保护属性,ruby-on-rails,ruby,ruby-on-rails-3,devise,Ruby On Rails,Ruby,Ruby On Rails 3,Devise,我已经努力解决这个问题一段时间了,尝试了很多选择,观看了和Railscapsts,并在他们的Github页面上尝试了Desive解决方案,但找不到解决这个问题的方法 我使用的是Rails 3.2 我有一个有很多地址的用户 用户模型: class User < ActiveRecord::Base rolify # Include default devise modules. Others available are: # :token_authenticatable, :co

我已经努力解决这个问题一段时间了,尝试了很多选择,观看了和Railscapsts,并在他们的Github页面上尝试了Desive解决方案,但找不到解决这个问题的方法

我使用的是Rails 3.2

我有一个有很多地址的用户

用户模型:

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :stripe_token, :coupon, :addresses_attributes
  attr_accessor :stripe_token, :coupon
  before_save :update_stripe
  before_destroy :cancel_subscription

  has_many :addresses
  accepts_nested_attributes_for :addresses, :reject_if => :all_blank, :allow_destroy => true

  def update_plan(role)
    self.role_ids = []
    self.add_role(role.name)
    unless customer_id.nil?
      customer = Stripe::Customer.retrieve(customer_id)
      customer.update_subscription(:plan => role.name)
    end
    true
  rescue Stripe::StripeError => e
    logger.error "Stripe Error: " + e.message
    errors.add :base, "Unable to update your subscription. #{e.message}."
    false
  end

  def update_stripe
    return if email.include?(ENV['ADMIN_EMAIL'])
    return if email.include?('@example.com') and not Rails.env.production?
    if customer_id.nil?
      if !stripe_token.present?
        raise "Stripe token not present. Can't create account."
      end
      if coupon.blank?
        customer = Stripe::Customer.create(
          :email => email,
          :description => name,
          :card => stripe_token,
          :plan => roles.first.name
        )
      else
        customer = Stripe::Customer.create(
          :email => email,
          :description => name,
          :card => stripe_token,
          :plan => roles.first.name,
          :coupon => coupon
        )
      end
    else
      customer = Stripe::Customer.retrieve(customer_id)
      if stripe_token.present?
        customer.card = stripe_token
      end
      customer.email = email
      customer.description = name
      customer.save
    end
    self.last_4_digits = customer.cards.data.first["last4"]
    self.customer_id = customer.id
    self.stripe_token = nil
  rescue Stripe::StripeError => e
    logger.error "Stripe Error: " + e.message
    errors.add :base, "#{e.message}."
    self.stripe_token = nil
    false
  end

  def cancel_subscription
    unless customer_id.nil?
      customer = Stripe::Customer.retrieve(customer_id)
      unless customer.nil? or customer.respond_to?('deleted')
        if customer.subscription.status == 'active'
          customer.cancel_subscription
        end
      end
    end
  rescue Stripe::StripeError => e
    logger.error "Stripe Error: " + e.message
    errors.add :base, "Unable to cancel your subscription. #{e.message}."
    false
  end

  def expire
    UserMailer.expire_email(self).deliver
    destroy
  end

end
地址模型

class Address < ActiveRecord::Base
  attr_accessible :country, :county, :first_line, :postcode, :second_line, :town_city
  #validates :country, :user_id, :county, :first_line, :postcode, :town_city, presence: true
  belongs_to :user
end
根据Desive Github页面上的建议,我已尝试将以下内容放入我的应用程序控制器中:

  def new

    @plan = params[:plan]
    if @plan && ENV["ROLES"].include?(@plan) && @plan != "admin"

      super

    @user.addresses.build

    else
      redirect_to root_path, :notice => 'Please select a subscription plan below.'
    end
  end
class AddressesController < ApplicationController
def create
end

def update
end
end
  before_filter :configure_permitted_parameters, if: :devise_controller?

无济于事。(我还试着允许“地址”和“地址”)/

任何解决方案都值得赞赏。(顺便说一句,如果您注意到其他不是最佳的代码,那么您最好了解这一点,因为您非常渴望养成好习惯)


感谢您接受嵌套的属性,您需要创建表单,然后创建以下参数散列

{:user=> { :addresses_attributes => [ 0 => {:street => "value" ...}, 1=> {:street=>"value2"}]
为了做到这一点,您需要使用

<%= fields_for :addresses do |form| %>
我认为在调用super之前需要建立地址关联

我改变了:

 def new
    @plan = params[:plan]
    if @plan && ENV["ROLES"].include?(@plan) && @plan != "admin"
    super
    else
      redirect_to root_path, :notice => 'Please select a subscription plan below.'
    end
  end
致:

原来

    @user.addresses.build
需要在设计新注册控制器的两行之间


我需要通过以适当的方式覆盖注册控制器来对此进行进一步优化。

尝试
而不是
?@scaryguy这样做会导致地址字段不显示在控制器的视图中,您还应该添加类似于
@user.addresses.build
@scaryguy的内容谢谢-我已经准备好了。我已经编辑了我的问题以包括控制器。这感觉很接近。我只是不知道把@user.address.build行放在哪里…把@user.address.build放在超级链接之前。参见上面的控制器代码。这个答案很有用。我必须定制控制器使其工作,但我们现在就在那里。-谢谢
<%= fields_for :addresses do |form| %>
def new
  @user = User.new
  @user.addresses.build
  super
end
 def new
    @plan = params[:plan]
    if @plan && ENV["ROLES"].include?(@plan) && @plan != "admin"
    super
    else
      redirect_to root_path, :notice => 'Please select a subscription plan below.'
    end
  end
  def new
    @plan = params[:plan]
    if @plan && ENV["ROLES"].include?(@plan) && @plan != "admin"

    build_resource({})
    @user.addresses.build
    respond_with self.resource

    else
      redirect_to root_path, :notice => 'Please select a subscription plan below.'
    end
  end
    @user.addresses.build