简体   繁体   English

Rails 4-以表格形式访问嵌套模型的属性

[英]Rails 4 - Access attributes of nested model in form

I have Account model which have a has_many relationship with User model: 我的帐户模型与用户模型具有has_many关系:

class Account < ActiveRecord::Base
  has_many :users, -> { uniq }
  accepts_nested_attributes_for :users


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

  belongs_to :account

I added avatar attribute to User model using paperclip. 我使用回形针将头像属性添加到用户模型。

I want each user to have access to the common account settings, and inside it having the possibility to upload his/her own avatar. 我希望每个用户都可以访问通用帐户设置,并且可以在其中上传自己的头像。

I use simple_form so I tried this: 我使用simple_form,所以我尝试了以下方法:

<%= simple_form_for current_account, :html => { :multipart => true } do |f| %>

    <%# here come account settings %>
    <%= f.input :time_zone, :label => t(".timezone"), 
                 :
                 :

    <%# here I need to access current user attributes %>
    <%= f.simple_fields_for :user, current_account.users.first do |user_form| %>
        <%= user_form.file_field :avatar, :error => false %>
    <% end %>

<% end %>

First problem: 第一个问题:
I need some logic to access current_user instead of current_account.users.first . 我需要一些逻辑来访问current_user而不是current_account.users.first Since there is a superadmin which can access all accounts, use current_user is not enough. 由于有一个超级用户可以访问所有帐户,因此仅使用current_user是不够的。

Second (and bigger) problem: 第二个(以及更大的)问题:
I added in my controller the avatar parameter to the whitelist: 我在控制器中将头像参数添加到了白名单:

def allowed_params
  params.require(:account).permit(:time_zone, :logo, :description, user: [:avatar])
end

When I try to update my model: 当我尝试更新模型时:

if current_account.update(allowed_params)

I get this error: 我收到此错误:

unknown attribute: user

I also tried: 我也尝试过:

params.require(:account).permit(:language, :time_zone, :logo, :description, :user_attributes => [:avatar])

and: 和:

params.require(:account).permit(:language, :time_zone, :logo, :description, :users_attributes => [:avatar])

(in plural) (以复数形式)

but since I use ActionController::Parameters.action_on_unpermitted_parameters = :raise I get: 但由于我使用ActionController::Parameters.action_on_unpermitted_parameters = :raise我得到了:

found unpermitted parameters: user

It must be something very easy, some help please? 这一定很简单,请帮忙吗?

Ok, got it!! 好的,我知道了!!

The problem is the one-to-many relationship and the way I tried to access a single instance of user. 问题是一对多关系以及我尝试访问单个用户实例的方式。 The correct way to do it is: 正确的方法是:

<% current_account.users.each_with_index do |user, index|%>
    <%= f.simple_fields_for :users, user do |user_form| %>
        <%= user_form.file_field :avatar, :error => false %>
    <% end %>
<% end %>

As you can see, the iteration should be done over the relation, and only when having a single instance "in hand" we can user the simple_fields_for . 如您所见,应该在关系上进行迭代,并且只有在“拥有”一个实例的情况下,我们才能使用simple_fields_for

Also, notice that the first parameter passed to simple_fields_for is :users and not :user , since this is a one-to-many relationship. 另外,请注意传递给simple_fields_for的第一个参数是:users而不是:user ,因为这是一对多的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM