html - 如何在注册前提交 Rails 表单

标签 html ruby-on-rails ruby

我有一个与公司相关的任务模型。我希望公司能够在主页上注册之前填写表格以发布任务。公司在提交表单后应该会被重定向到注册页面,然后任务会自动创建并与公司相关联。

我正在为公司模型使用设计。

The form should look like this

静态页面 Controller :

def home
end

任务 Controller :

def create
  @task = current_company.tasks.build(task_params)
  if @task.save
    redirect_to @task
  else
    render 'new'
  end
end

def new
  @task = Task.new
end

private

  def task_params
    params.require(:task).permit(:name, :description, :pay, files: [], course_ids: [])
  end

任务模型:

belongs_to :company

公司模式:

has_many :tasks

任务/new.html.erb:

<h2>Create Task</h2>
</div>
<%= form_for(@task) do |f| %>
  <div class="space">
    <%= f.text_field :name, placeholder: "Task Name", class: "text-field" %>
  </div>
  <div class="space">
    <%= f.text_area :description, placeholder: "Add Description", class: "text-field", rows: 10  %>
  </div>
  <div class="space">
    <%= f.number_field :pay, placeholder: "Task Pay in USD", class: "text-field"%>
  </div>

  <div class="space">
    <label class="file-field">
        <%= f.file_field :files, multiple: true %>
    </label>
  </div>
  <div class="space">


  <%= f.submit "Post", class: "btn button", style: " width: 70%; padding-top: 10px; padding-bottom: 10px; margin-bottom: 10px; font-size: 1.25em;" %>
<% end %>

Static_pages/home.html.erb:

<div class="container-fluid container-1">
  <div class="row col-centered">
    <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6" style="text-align: left;">
    </div>
    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5">
      <h2 class="subtitle3"> Find Talented Freelancers</h2>
      <%= link_to "Sign Up", companysignup_path, class: "btn button", style: "width: 40%; padding-top: 10px; padding-bottom: 10px; font-size: 1.25em; float: left;" %>
      <%= link_to "Log In", companylogin_path, class: "btn button", style: "width: 40%; padding-top: 10px; padding-bottom: 10px; font-size: 1.25em; float: left; margin-left: 5%;" %>
    </div>
  </div>
</div>

最佳答案

一个非常常见的解决方案是提供“访客帐户”:

class AddStatusToCompanies < ActiveRecord::Migration[5.0]
  def change
    add_column :companies, :status, :integer, default: 0
  end
end


class Company < ApplicationRecord
  # ...
  enum status: [:default, :guest, :registered]
  validates :name, length: { minimum: 2 }, unless: :guest?

  def password_required?
    if guest?
      false
    else
      super
    end
  end
end

这基本上只是一个带有 ActiveRecord::Enum 的模型我们用来切换验证。

让我们更改 Controller 以创建访客记录:

def create
  @company = current_company || create_guest_company
  @task = @company.tasks.build(task_params)
  if @task.save
    if @company.guest?
      redirect_to "/your/registration/path"
    else
      redirect_to @task
    end
  else
    render :new
  end
end

private 
def create_guest_company
  company = Company.create!(status: :guest, email: "guest-#{SecureRandom.uuid}@example.com")
  sign_in company
  company
end

这还需要对您的注册 Controller 进行大量更改,以支持更新现有记录或创建单独的路由和 Controller 来处理完成的访客帐户。这是冗长教程的主题,而不是 stackoverflow 的答案。

您还需要一个循环后台任务来清理不完整的“转化”(在营销意义上)。

namespace :companies do
  desc "Remove guest companies more than a week old."
  task :cleanup => :environment do
    Company.guest.where("created_at < ?", 1.week.ago).destroy_all
  end
end

关于html - 如何在注册前提交 Rails 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57340699/

相关文章:

javascript - 货币数据表格式

javascript - 在 div 标签附近出现错误

jquery - 在 Rails 应用程序中使用 jQuery 进行 Ajax 调用

ruby-on-rails - Rubocop:错误 - rubocop 返回退出代码 2

javascript - Malihu 自定义滚动条 - 滚动到 id 插件在页面中不起作用

html - :hover not working on my svg path

ruby-on-rails - 不明确的列名 : error not working with tags

ruby-on-rails - Paypal future 付款

css - 没有 Ruby 的客户端如何使用 SASS 查看网页?

ruby-on-rails - Rails + Devise 嵌套注册表未定义方法 'build_address'