0

我需要一些帮助来为具有多个模型的控制器设置表单和创建操作。目前,表单将提交,但没有处理或记录任何内容,也没有显示错误。

这个想法是这样的:用户可以注册和管理他的帐户。然后他可以创建一个“关心”帐户,基本上只需要一个名字即可开始,并且不需要其他验证,因为子帐户不会使用电子邮件/密码登录。如果子账号以后要访问,父账号可以为其设置邮箱/密码,它会成为一个完整的账号。这个父账户,经理,也可以邀请另一个账户,也许是配偶,作为这个孩子账户的另一个经理。

在此先感谢您的帮助

账户模型

class Account < ActiveRecord::Base

validates :email, presence: true, 
                format:     { with: VALID_EMAIL_REGEX},
                uniqueness: { case_sensitive: false },
                length:     { maximum: 32 },
                unless: :child?

has_many :relationships, foreign_key: "manager_id", dependent: :destroy
has_many :cares, through: :relationships, source: :care
has_many :reverse_relationships, foreign_key: "care_id", class_name: "Relationship", dependent: :destroy
has_many :managers, through: :reverse_relationships, source: :manager
...
def child?
  false
end

class CareUser < Account

 def child?
   true
 end
end

关系模型

class Relationship < ActiveRecord::Base
 attr_accessible :care_id, :manager_id, :type

 belongs_to :manager, class_name: "Account"
 belongs_to :care, class_name: "Account"

 validates :manager_id, presence: true
 validates :care_id, presence: true
end

关心控制器

class CaresController < ApplicationController
  def index
     @cares = current_user.cares
  end

  def new
    @user = CareUser.new
    @care = Relationship.new
  end

  def create
    @user = CareUser.new(params[:first_name])
    logger.info @user.inspect
    if @user.save(false)
      @care = current_user.relationships.build(current_user.id, @user.id, params[:relation])
      if @care.save
        flash[:success] = "New care has been successfully added."
      else
        flash[:error] = "Failed"
        render action: new
      end
    else
      flash[:error] = "Failed adding user"
      render action: new
    end

  end

  def show
  end

  def edit
  end

  def update
  end

  def destroy
  end
end

@user = CareUser.new我得到uninitialized constant CaresController::CareUser时,子类......但如果将其设置@user = Account.new为父类,则不会显示错误。

提交表单,cares/new.html.erb

不知道设置表单的正确方法是什么,但由于提交到两个模型,我使用form_tag而不是form_for. 目前我似乎无法正确提交表单,不确定问题出在表单、模型还是控制器中。

<div class="row">
  <div class="span6 offset3">
    <h1>New care</h1>
    <%=  form_tag("/cares", method: :post) do %>
        <% if @user.errors.any? %>
            <div id="error_explanation">
              <div class="alert alert-error">
                The form contains <%= pluralize(@user.errors.count, "errors") %>
              </div>
              <ul>
                <% @user.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                <% end %>
              </ul>
            </div>
        <% end %>
        <% if @care.errors.any? %>
            <div id="error_explanation">
              <div class="alert alert-error">
                The form contains <%= pluralize(@care.errors.count, "errors") %>
              </div>
              <ul>
                <% @care.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                <% end %>
              </ul>
            </div>
        <% end %>

        <%= label_tag(:first_name, "First name:") %>
        <%= text_field_tag(:first_name) %>

        <%= label_tag(:relation, "Relationship") %>
        <%= select_tag(:relation, options_for_select([ ["Son", "1"], ["Daughter", "2"], ["Father", "3"], ["Mother", "4"], ["Grandson", "5"], ["Granddaughter", "6"], ["Grandfather", "7"], ["Grandmother", "8"] ])) %>


        <br>
        <%= submit_tag("Create care account") %>


    <% end %>
  </div>

</div> 
4

0 回答 0