【问题标题】:Rails 5.1.2 Wicked Gem and Nested AttributesRails 5.1.2 Wicked Gem 和嵌套属性
【发布时间】:2017-09-02 15:25:04
【问题描述】:

Ruby: 2.4.0
Rails: 5.1.2


大家好, 我对 Rails 并不完全陌生,但绝对不是专家。

我要做的是创建一个具有嵌套属性的邪恶巫师。

我已经通过 Google、GitHub 和 StackOverflow 进行了搜索,但除了 thisthat 之外,没有找到任何东西。

两者都不起作用。

我拥有的是以下内容:

-_-_-_-_-_-_-_-_-_-_-_-_-_ 编辑!!!-_-_-_-_-_- _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

问题是,我还没有定义创建操作 -.-' 我还必须修改一些变量名。在我看来,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

观看次数

welcome.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。

devise 运行良好,重定向到邪恶的控制器也是如此。 wicked 一如既往地正常工作,路线也设置正确,但这次我希望将我的“能力”信息存储到相关的 account_information 表中。

在我按下“Weiter”(德语中的“继续”)按钮后,除了网站重新呈现之外,什么也没有发生。这是我的控制台输出的内容:

由 UserStepsController#update 处理为 HTML 参数: {"utf8"=>"✓", "authenticity_token"=>"uYRJyCeBGyyDcWUtIj62fmT9oMpTnUQ3p+CSi3n8tSKKLguB1j/CPZaeuZCcmpCoBjJDKY6yz7/Z2wXfAO7YBg==", "user"=>{"account_information_attributes"=>{"competence"=>"1"}}, "commit"=>"Weiter", "id"=>"welcome"} 用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?按“用户”订购。“ID” 升速限制? [["id", 12], ["LIMIT", 1]] (0.0ms) 开始交易 AccountInformation Load (0.1ms) SELECT "account_informations".* FROM "account_informations" 在哪里 "account_informations"."user_id" = ? 限制 ? [["user_id", 12], ["LIMIT", 1]] (0.1ms) 回滚 事务 (0.0ms) 开始事务 (0.0ms) 回滚 交易

【问题讨论】:

标签: ruby-on-rails devise nested-forms wicked-gem ruby-on-rails-5


【解决方案1】:

根据 Wicket 文档:

class AfterSignupController < ApplicationController
  include Wicked::Wizard

  steps :confirm_password, :confirm_profile, :find_friends

  def show
    @user = current_user
    case step
    when :find_friends
      @friends = @user.find_friends
    end
    render_wizard
  end
end

您的控制器中应该有一个case step,我在您当前的代码中没有看到。

看起来像重定向问题

【讨论】:

  • 感谢您的帮助。我将案例步骤的东西添加到我的控制器中,但重定向似乎仍然不起作用,我不知道为什么。我还必须在我的步骤控制器中执行“@account_information = @user.build_account_information”,如果我删除 build_ 表单将不会显示
  • 在您的代码中添加调试(老派logger.debug MESSAGE 以检查您的应用程序中发生了什么。
  • 我没有获得任何成功:S 我只是无法弄清楚是什么阻止了我的代码。也许它的路线?我是否必须在我的路线中像resources :user do resources :account_information end 那样做std,还是在我的嵌套形式中像d.fields_for :account_information_attributes 这样?
  • 似乎没有连接到字段,因为它们与路由无关。根据您提供的信息,这看起来像是一个路由问题。 routes.rb 或您的控制器可能会覆盖预期的行为
  • 我发现了问题。我将更新我的帖子,向您展示出了什么问题!谢谢你的帮助!
【解决方案2】:

我找到了解决方案。问题是缺少创建操作和其他一些东西。我编辑了帖子,所以你可以阅读ist

【讨论】:

猜你喜欢
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
相关资源
最近更新 更多