我正在使用设计进行注册,并尝试嵌套表单进行注册,只有该部分不会出现在表单中
用户模型
class User < ActiveRecord::Base
has_many :languages_users
has_many :languages, :through => :languages_users
accepts_nested_attributes_for :languages_users
end
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_sign_up_params, only: [:create]
def new
build_resource({})
resource.languages_users.build
respond_with self.resource
end
def create
super
end
protected
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
end
sign_up表单的相关部分
<%= form_for(resource, as: resource_name, url: "users/registration_path(resource_name)") do |f| %>
<%= f.fields_for :languages_users 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 %>
</div>
<% end %>
<% end %>
相关路线
Rails.application.routes.draw do
resources :languages
resources :languages_users
devise_for :users
end
任何见解都将受到赞赏!
答案 0 :(得分:0)
应该按原样运作,你确定不是:
resource
控制器操作方法和视图registration#new
实例
resource.languages
,这将触发您的中间language_users
关系的SQL查询并将其刷新 Rails也可能会混淆,因为您的表名模式languages_users
专门用于定义has_and_belongs_to_many
关系中的中间表,尝试重命名模型或使用此最后一种关系类型(如果它更适合您的需求
答案 1 :(得分:-1)
<%= form_for(resource, as: resource_name, url: "users/registration_path(resource_name)") do |f| %>
<% resource.languages_users.each do |languages_user| %>
<%= f.fields_for languages_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 %>
</div>
<% end %>
<% end %>
<% end %>