【问题标题】:cocoon with devise not working Nested Form茧与设计不工作嵌套形式
【发布时间】:2015-02-16 19:02:25
【问题描述】:

我有一个新问题,尝试用茧实现设计,但它不保存在数据库中。请帮我 !!! :D

用户模型:

class User < ActiveRecord::Base
  has_many :telefonos
  accepts_nested_attributes_for :telefonos, :reject_if => :all_blank,  :allow_destroy => true

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

电话机型号:

class Telefono < ActiveRecord::Base
  belongs_to :user
end

在我的用户控制器中:

def secure_params
    params.require(:user).permit(:telefonos_attributes: [:id, :telefono, :_destroy])
end

我的应用控制器

Class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_filter :configure_permitted_parameters, if: :devise_controller?

    protected

        def configure_permitted_parameters
             devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:nombre, :email, :password) }
            devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :current_password, :region_id, :ciudad_id, :nombre, telefonos_attributes: [:telefono, :user_id]) }
        end
     end

我的编辑视图:

<h3>Edit <%= resource_name.to_s.humanize %></h3>

  <%= simple_form_for resource, :as => resource_name, url: registration_path(resource_name), :html => { :method => :put} do |f| %>

    <div class="form-group">
      <%= f.input :nombre, :autofocus => true, class: 'form-control' %>
      <%= f.association :region, :include_blank => false %>
      <%= f.association :ciudad, :include_blank => false %>
    </div>
    <h1>Telefono</h1>
    <div id="telefonos">
    <%= f.simple_fields_for :telefonos do |t| %>
        <%= render 'telefono_fields', :f => telefono %>
    <% end %>
    <div class="links">
      <%= link_to_add_association 'add telefono', f, :telefonos %>
    </div>
  </div>
<%= f.submit 'Update' %>
<% end %>

我的 _telefono_fields.html.erb

<div class="nested-fields">
  <%= f.input :telefono %>
  <%= link_to_remove_association "remove telefono", f %>

问候,

【问题讨论】:

  • telefonos_attributes 中,您应该还有两个属性id_destroy。这只是为了您的信息。这可能是导致此问题的原因,也可能不是。试一试。您是否在控制台中收到任何错误检查?
  • 谢谢,但不会在控制台中产生错误! :/ 你有教程吗?
  • 不允许的参数:nombre, region_id, ciudad_id, telefonos_attributes

标签: ruby-on-rails ruby-on-rails-4 devise ruby-on-rails-3.2 cocoon-gem


【解决方案1】:

我以不同的方式做了同样的事情。我认为您收到了unpermitted parameters,因为应用程序控制器方法不起作用。但我有一个解决方案给你。首先在终端中运行这个命令:

rails g devise:controllers users

除了users,您可以使用任何作为范围的东西。这意味着它将生成像app/controllers/users/[all controllers] 这样的设计控制器。然后在你的路由文件中这样做:

devise_for :users, controllers: {
    registrations: 'users/registrations'
  }

它将使用生成的registrations_controller。现在在您生成的注册控制器中这样做。以下方法将在 cmets 中编写,只需取消注释并根据您的需要进行编辑:

class Users::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_sign_up_params, only: [:create]
  before_filter :configure_account_update_params, only: [:update]

protected


  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up){ |u| u.permit(:email, :password, :password_confirmation, :nombre) }
  end

  def configure_account_update_params
    devise_parameter_sanitizer.for(:account_update){ |u| u.permit(:email, :password, :current_password, :password_confirmation, :region_id, :ciudad_id, :nombre, telefonos_attributes: [:id, :telefono, :user_id], :_destroy) }
  end

我假设您在注册时只需要提到的属性。如果您还需要任何其他属性,请添加它们。

默认情况下,该设计需要current_password 进行任何编辑。如果您使用的是设计生成的视图,那么它应该包含current_password 的字段。但我在你的表格中没有看到它。因此,如果您想跳过此操作并且不希望用户每次都输入密码,则在 registrations_controller 中仅将其添加到 protected 范围内:

def update_resource(resource, params)
  resource.update_without_password(params)
end

这对我有用。希望这对你也有帮助。

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多