该页面未正确重定向以用于注册表单对象

时间:2015-10-15 18:20:51

标签: ruby-on-rails

我试图根据思想机器人的博客文章实施用户注册一段时间后进行了一些更改,但无法让它工作: https://robots.thoughtbot.com/activemodel-form-objects

尝试访问以查看结果"页面未正确重定向":

应用程序/视图/注册/ new.html.slim

= form_for @registration, url: registration_index_path do |f|

  = f.text_field :company_name
  = f.text_field :user_first_name
  = f.text_field :user_last_name
  = f.text_field :user_email
  = f.password_field :user_password

  = f.submit 'Register'

配置/ routes.rb中

resources :users

其他相关部分:

应用程序/控制器/ registration_controller.rb

class RegistrationController < ApplicationController

  def new
    @registration = Registration.new
  end

  def create
    @registration = Registration.new(params[:registration])

    if @registration.save
      session[:user_id] = @registration.user.id

      redirect_to root_path, notice: 'Successfully signed in'
    else 
      render :new
    end 
  end
end

应用程序/模型/ registration.rb

class Registration
  include ActiveModel::Model

  attr_accessor :company_name, :user_first_name, :user_last_name, 
  :user_email, :user_password

  validates :company_name, presence: true
  validates :user_first_name, :user_last_name, presence: true
  validates :user_email, presence: true
  validates :user_password, presence: true

  attr_reader :company, :user

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

  private

  def persist!
    @company = Company.create!(name: company_name)

    @user = User.create!(company: company, first_name: user_first_name,  
    last_name: user_last_name,email: user_email, password: user_password, 
    password_confirmation: user_password)
  end
end

由于

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  private

  def require_signin!
    if current_user.nil?
      flash[:error] =
       "You need to sign in first"
      redirect_to login_path
    end
  end
  # helper_method :require_signin!

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user

  def current_company
      @current_company ||= current_user.company
    end
  end
  helper_method :current_company

end

0 个答案:

没有答案