红宝石: 2.4.0
轨道: 5.1.2
大家好,我对 Rails 并不完全陌生,但绝对不是专家。
我要做的是创建一个具有嵌套属性的邪恶向导。
我已经通过 Google、GitHub 和 StackOverflow 进行了搜索,但除了this和that之外什么都没找到。
两者都不起作用。
我所拥有的是以下内容:
-_-_-_-_-_-_-_-_-_-_-_-_-_ 编辑 !!!-_-_-_-_-_-_-_-_-_-_ -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
问题是,我还没有定义创建操作 -.-' 我还必须修改一些变量名。在我看来,rails 有时与复数有点混淆。幸运的是终于能够做这个超级简单的事情:D
楷模
user.rb(来自设计 gem)
class User < ApplicationRecord
has_one :accountinfo
accepts_nested_attributes_for :accountinfo, update_only: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :password, :email, :accounttype
end
account_information.rb
编辑:accountinfo.rb
我想要存储用户信息的模型不会使我的用户表超载(一切都很好)
class AccountInformation < ApplicationRecord
belongs_to :user
end
我已经使用了 wicked 几次,它总是有效,但前提是我将所有信息存储在用户模型中......这次我不想这样做,因为它有更好的结构和东西。
控制器
users_controller.rb
class UsersController < ApplicationController
def index
redirect_to root_path
end
def create
@user = User.create( user_params )
end
def show
@user = User.find(params[:id])
if user_signed_in?
render 'show'
else
redirect_to root_path
end
end
private
def user_params
params.require(:user).permit(:first_name, :accounttype, :accountinfo, accountinfos_attributes:[:user_id, :competence])
end
end
user_steps_controller
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :welcome
before_action :authenticate_user!
def show
@user = current_user
render_wizard
end
def create
@accountinfo = @user.accountinfo.create(user_params)
end
def update
@user = current_user
@user.update(user_params)
render_wizard @user
end
private
def user_params
params.require(:user).permit(:accountinfo,accountinfo_attributes:[:id, :competence])
end
private
def redirect_to_finish_wizard_path
redirect_to root_path, notice: "Danke für deine Zeit!"
end
end
private
def user_params
params.require(:user).permit(:accounttype,
account_information_attributes:[:id, :competence])
end
private
def redirect_to_finish_wizard_path
redirect_to root_path, notice: "Danke für deine Zeit!"
end
end
意见
欢迎.html.erb
<%= form_for @user, url: wizard_path do |f| %>
<%= f.fields_for(:accountinfo) do |builder| %>
<div class="m-wizard__choose2 m-wizard__choose2--border-right">
<label>
<%= builder.radio_button :competence, 1, class: "input-hidden" %>
<i class="icon-people-male h1"></i>
<p class="h6 m-text--bold">Designer</p>
</label>
</div>
<div class="m-wizard__choose2">
<label>
<%= builder.radio_button :competence, 2, class: "input-hidden" %>
<i class="icon-people h1"></i>
<p class="h6 m-text--bold">Texter</p>
</label>
</div>
<% end %>
<div class="m-text--center">
<%= f.submit "Weiter", class: "m-button" %>
</div>
<% end %>
也许你已经找到了一些东西......
我希望我的用户在注册后来到welcome.html.erb。
设计运行良好,重定向到邪恶的控制器也是如此。wicked 一如既往地正常工作,路线也设置正确,但这次我希望将我的“能力”信息存储到关联的 account_information 表中。
在我按下“Weiter”(德语中的“继续”)按钮后,除了网站重新呈现之外,什么也没有发生。这是我的控制台输出的内容:
UserStepsController#update 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"uYRJyCeBGyyDcWUtIj62fmT9oMpTnUQ3p+CSi3n8tSKKLguB1j/CPZaeuZCcmpCoBjJDKY6yz7/Z2wXfAO7YBg==", "user"=>{"account_petence=>{ "=>"1"}}, "commit"=>"Weiter", "id"=>"welcome"} 用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id " = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 12], ["LIMIT", 1]] (0.0ms) 开始交易 AccountInformation Load (0.1ms) SELECT "account_informations".* FROM "account_informations" WHERE "account_informations"。