连接表

时间:2016-03-26 16:50:17

标签: ruby-on-rails nested-forms jointable

我正在尝试将嵌套表单添加到Devise注册表单中。 我的3个表是用户(设计),语言,它们都加入了Languages_Users

它注册新用户,但不保存Languages_Users,但我看到它在params中传递。还出现了一个新问题 - 不再保存头像文件。我不确定这些是否相关(如果没有,不要担心解决它)。

**更新 - 这是我当前的日志 - 我现在认为由于languages_users没有收到user_id而导致的问题

"languages users can't be blank'. In my logs though, it says 'SELECT 1 AS one FROM "languages_users" WHERE ("languages_users"."user_id" IS NULL AND "languages_users"."language_id" = 2) LIMIT 1' Is this an issue with not passing the user_id?

**

User.rb

class User < ActiveRecord::Base
  has_many :languages_users
  has_many :languages, :through => :languages_users
  accepts_nested_attributes_for :languages_users

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

  belongs_to :role

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"

  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
  validates_presence_of :first_name, :last_name, :location, :nationality, :bio
 end

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
 def new
 build_resource({})
  resource.languages_users.build  
 respond_with self.resource 
end

def create
  super
end
 private
 def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_user_attributes: [:id, :language_id, :user_id, :level]) }
end
end
注册中的

: new.html.erb

<h2>Sign up</h2>

<%= form_for(resource, as: resource, url: registration_path(resource)) do |f| %>
  <%= devise_error_messages! %>
  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: true %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :location %><br />
    <%= f.text_field :location %>
  </div>
   <div class="field">
    <%= f.label :nationality %><br/>
    <%= f.text_field :nationality %>
  </div>
  <div class="field">
    <%= f.label :bio %><br />
    <%= f.text_field :bio %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="form-group">
        <%= f.label :avatar, class: 'col-sm-2 control-label'  %>
        <div class="col-sm-6">
            <%= f.file_field "user[avatar]" %>
        </div>
  </div>
    <%= f.fields_for :langauges_user do |lu| %>
    <br>
      <div class="fields"> 
      <%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
      <%= lu.hidden_field :level, value: 1 %>
    <% end %>
  </div>  
  <br>
  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

日志:

    Started POST "/users" for ::1 at 2016-03-26 13:30:40 -0400
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"x1F0kXqKN2/uu7S6BLxyBgSatcVWiSOLASEYYJ7ZF0b3d8V8O+FVQAO0yhjTJ2LImI+Xy4j7Rn+SvlYjV07mrA==", "user"=>{"first_name"=>"John", "last_name"=>"Smith", "location"=>"NYC", "nationality"=>"American", "bio"=>"hello", "email"=>"johns@gmail.com", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f940b16a4f8 @tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160326-11607-1fg5ncg.JPG>, @original_filename="IMG_4573.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4573.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "langauges_users"=>{"language_id"=>"16", "level"=>"1"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: user, langauges_users

还是一个新手,所以如果您希望我提供更多信息,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该将languages_user更改为languages_users

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u|
  u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [:id, :language_id, :user_id, :level]) }
end

同样在fields_for

<%= f.fields_for :languages_users do |lu| %>