创建新记录无法保存relationship_id

时间:2015-05-05 16:24:07

标签: ruby-on-rails ruby ruby-on-rails-4

组织有许多成员,并且两个模型之间已建立关系。允许组织的成员为该特定组织创建其他成员。这很好用。系统管理员可以转到任何组织的个人资料,然后点击“创建新成员”。创建一个组织的新成员。但是,当前对于admin,organization_id不会使用新创建的成员进行保存,从而导致错误。有没有人知道为什么organization_id没有保存?

会员控制员:

> a <- data.frame(a = 1:3)
> b <- data.frame(b = 1:2)

> some.magic.bind(a, b) # what function to use here?

   a  b
1  1 1
2  2 2
3  3 NA

成员新观点:

  def new
    if current_user.admin?
      if params[:organization_id].nil?
        flash[:danger] = "Please select an organization first"
        redirect_to organizations_path
      else
        @organization = Organization.find(params[:organization_id])
        @member = @organization.members.build
      end
    else
      @member = current_organization.members.build
    end
  end

  def create
    if current_user.admin?
      @member = Member.new(new_params)
    else
      @member = current_organization.members.build(new_params)
    end
    if @member.save
      @member.send_activation_email
      flash[:success] = "A confirmation email will be sent to the new user."
      redirect_to member_url(@member)
    else                            
      render 'new'
    end
  end

private
def new_params
  params.require(:member).permit(:email,
                                  :username,
                                  :password,
                                  :password_confirmation)
end

组织配置文件视图包含指向成员新视图的以下链接:

<%= form_for(@member) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <% if current_user.admin? %>
    <%= f.text_field :organization_id, placeholder: 'Organization id', autocomplete: 'off', class: 'form-control', disabled: "disabled" %>
  <% end %>

  <%= f.email_field :email, placeholder: 'Email', autocomplete: 'off', class: 'form-control' %>
  <%= f.text_field :username, maxlength: 15, placeholder: 'Username', autocomplete: 'off', class: 'form-control' %>
  <%= f.password_field :password, placeholder: 'Password', autocomplete: 'off', class: 'form-control' %>
  <%= f.password_field :password_confirmation, placeholder: 'Confirm password', autocomplete: 'off', class: 'form-control' %>

  <%= f.submit "Sign up", class: "formbutton btn btn-default" %>
<% end %>

部分成员模型:

<%= link_to image_tag("add.png", title: "add member", height: '15'), new_member_path(organization_id: @organization.id) %>

2 个答案:

答案 0 :(得分:0)

def new_params,您不允许:organization_id

如果出于安全原因,您可以按如下方式更新您的创建操作:

def create
    if current_user.admin?
      @member = Member.new(new_params)
      @member.organization_id = params[:member][:organization_id]
    else
     ....
end

同样在您的表单中,如果禁用某个字段,则不会将其作为参数发送。您应为hidden_field添加:organization_id

<% if current_user.admin? %>
    <%= f.text_field :organization_id, placeholder: 'Organization id', autocomplete: 'off', class: 'form-control', disabled: "disabled" %>
    <%= f.hidden_field :organization_id, value: @organization.id %>

<% end %>

答案 1 :(得分:0)

问题原来是表格中的disabled: "disabled"。禁用字段不会在提交时发送。将其更改为readonly: "readonly"解决了它。请参阅:What's the difference between disabled="disabled" and readonly="readonly" for HTML form input fields?