注册时以嵌套形式传递user_id并保存两个模型

时间:2016-03-29 22:48:07

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

我的Devise注册表单上有一个嵌套表单,它填充了一个连接表。由于user_id为nil,因此未保存连接表信息。这是因为它们尚未登录,因此我无法传递current_user。如何以一种形式传递user_id以保存连接表(语言用户)?

new.html.erb的相关部分

 <%= 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>
  <%= f.fields_for :languages_users do |lu| %>
   <div class="fields">
     <%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
     <%= lu.hidden_field :level, value: 1 %>
     <%#= lu.hidden_field :user_id, value: current_user.id %>
  <% end %>
   </div>
   <div class="actions">
     <%= f.submit "Sign up" %>
   </div>
  <% end %>

registrations_controller.rb

    class Users::RegistrationsController < Devise::RegistrationsController
    before_filter :configure_permitted_parameters
    def new
         build_resource({})
          resource.languages_users.build(user_id: current_user)  
         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_users_attributes: [ :language_id, :user_id, :level]) }
  end
end

这是我的参数:

  {"utf8"=>"✓",
 "authenticity_token"=>"v/uM0+B7/XCUKmhyFGoEKqunCllXAK50uZN5f+TDpUsxlnIsgQVPbXhEb1q1P5pmG586iaULRROyarWzAzz12w==",
 "user"=>{"first_name"=>"Walsh",
 "last_name"=>"Costigan",
 "location"=>"w",
 "nationality"=>"United States",
 "bio"=>"w",
 "email"=>"walsh@seamlessdocs.com",
 "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fdd25023f78 @tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160403-6431-1hpthwr.JPG>,
 @original_filename="IMG_4574.JPG",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4574.JPG\"\r\nContent-Type: image/jpeg\r\n">},
 "languages_users_attributes"=>{"language_id"=>"18",
 "level"=>"1"},
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Sign up"}

更新**这是我目前的代码所在。它保存用户,但不保存languages_user模型

user.rb     after_create:create_languages_user

def create_languages_user
 @users = self.id
 @languages_user = LanguagesUser.new(@user)
end

注册管理员:

def new
 @user = User.new
 build_resource({})
 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_users_attributes: [ :language_id, :level]) }
end

3 个答案:

答案 0 :(得分:3)

很简单。只需将阻止传递给 if isValidEmail(textField.text) { println("Validate EmailID") } else { println("invalide EmailID") } 控制器

中的#create方法即可
Users::RegistrationsController

答案 1 :(得分:2)

您可以访问user_id回调中的after_create

after_create :example_method

def example_method
   NameOfModel.create(user_id: self.id, etc)
end

答案 2 :(得分:0)

你可以这样做

after_create :create_languages_user

def create_languages_user
 @languages_user = self.languages_users.create
end