Rails邪恶的宝石 - 在一个向导中使用两个(嵌套)模型
问题描述:
我想在用户注册成功后向我的向导使用邪恶宝石。Rails邪恶的宝石 - 在一个向导中使用两个(嵌套)模型
第一步是用户应该输入他的“普通信息”(生物和兴趣)。提交这些信息后,用户应输入一个特征(用户有很多特征)。
我遇到的问题如下:如何正确使用Wicked为第一步用户模型和第二步(属于用户)的特征模型创建表单?
我得到的错误是:
Can't mass-assign protected attributes: characteristics
我不想使用嵌套的属性,因为向导会在那里我会用他们的唯一地方。
这里是我的源代码:
邪恶 - 控制器:
#!/bin/env ruby
# encoding: utf-8
class AfterFirstSignInController < ApplicationController
include Wicked::Wizard
skip_before_filter :check_if_profile_complete, only: [:show, :update]
steps :general_infos, :characteristics
def show
@user = current_user
@characteristic = @user.characteristics.new
@title = t('controllers.after_first_sign_in_controller.show.title')
render_wizard
end
def update
@user = current_user
@characteristic = @user.characteristics.new
case step
when :general_infos
@user.attributes = params[:user]
render_wizard @user
else
@characteristic.attributes = params[:characteristic]
render_wizard @characteristic
end
end
end
“一般-相关信息形式的”
.container
.row
.span5.offset3
.progress.progress-info.progress-striped
.bar(style='width: 50%')
1/2
.page-header
%h1
= t('views.after_first_sign_in.general_infos.h1')
= form_for(@user, url: wizard_path, method: :put) do |f|
= render 'shared/error_messages', object: @user
.control-group
%label.control-label(for='bio')
= t('views.after_first_sign_in.general_infos.labels.bio')
.controls
= f.text_area :bio, id: 'bio', rows: '6'
.control-group
%label.control-label(for='interest_list')
= t('views.after_first_sign_in.general_infos.labels.interest_list')
.controls
= f.text_field :interest_list, id: 'interest_list'
.form-actions
= f.submit t('views.after_first_sign_in.general_infos.buttons.submit'), disable_with: t('views.after_first_sign_in.general_infos.buttons.submit'), class: 'btn btn-primary pull-right'
“特色型“
.container
.row
.span5.offset3
.progress.progress-info.progress-striped
.bar(style='width: 100%')
2/2
.page-header
%h1
= t('views.after_first_sign_in.characteristics.h1')
= form_for([@user, @characteristic], url: wizard_path, method: :put) do |f|
= render 'shared/error_messages', object: @characteristic
= f.fields_for :characteristics do |builder|
.control-group
%label.control-label(for='title')
= t('views.after_first_sign_in.characteristics.labels.title')
.controls
= builder.text_field :title, id: 'title'
.control-group
%label.control-label(for='description')
= t('views.after_first_sign_in.characteristics.labels.description')
.controls
= builder.text_area :description, id: 'description', rows: '6'
.form-actions
= f.submit t('views.after_first_sign_in.characteristics.buttons.submit'), disable_with: t('views.after_first_sign_in.characteristics.buttons.submit'), class: 'btn btn-primary pull-right'
= link_to t('views.after_first_sign_in.characteristics.buttons.general_infos_step'), after_first_sign_in_path(:general_infos), class: 'btn'
的路由文件
Test::Application.routes.draw do
scope '(:locale)' do
resources :users do
resources :characteristics
resources :followings, only: [:create, :destroy, :index]
resources :wall_entries, only: [:create, :destroy]
resources :messages, only: [:create, :destroy, :index]
end
resources :characteristics do
resources :votes, only: [:create, :destroy]
end
resources :after_first_sign_in
match '/auth/:provider/callback', to: 'sessions#create'
match '/auth/failure', to: redirect('/')
match '/sign_out', to: 'sessions#destroy', as: 'sign_out'
match '/change_locale', to: 'users#change_locale', as: 'change_locale'
match '/home', to: 'users#home', as: 'home'
match '/discover', to: 'users#discover', as: 'discover'
match '/terms_of_service', to: 'pages#terms_of_service'
match '/masthead', to: 'pages#masthead'
match '/privacy', to: 'pages#privacy'
root to: 'pages#index'
end
end
非常感谢您!
答
我猜你正在使用nested_attributes
为@user.characteristics
。如果是这样,那么你应该将以下添加到您的用户模型:
attr_accessible :characteristics_attributes
accepts_nested_attributes_for :characteristics
但也未尝不是在您的characteristics-form
这部分清楚,你可能想是这样的:
= form_for(@user, url: wizard_path, method: :put) do |f|
= render 'shared/error_messages', object: @user
= f.fields_for :characteristics do |builder|
需要注意的是,fields_for
可能会遍历每个特征单独和渲染为他们每个人的领域,所以你不需要关心@characteristic
我不知道还有什么其他问题,可怎么回事,因为我不熟悉的邪恶宝石,但那个错误r绝对表示它试图设置受保护的属性。你应该通过在http://guides.rubyonrails.org/security.html#mass-assignment或许这个问题以及http://stackoverflow.com/questions/10050797/rails-error-部分“质量分配”读集中分配保护属性。希望有所帮助。 – Tim
这有什么好运气?我试图创建一个类似的应用程序,我也遇到了同样的错误 – godzilla3000