我的索引不显示已通过其他模型检索的表单字段

时间:2018-04-14 07:26:07

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个用于帐户的铁路脚手架和另一个用于交易的脚手架。 Accounts模型作为在表单中创建的属性account_name。对于每个事务,表单然后提示用户选择事务所属的帐户,该帐户从Accounts account_name字段中检索。这显示完美,但保存事务时,帐户名称不会保存在数据库中或显示在索引中。编辑事务时,帐户名称字段设置为空白。 长期以来一直坚持这一点 - 任何建议都会非常感激。

我的帐户模式:

class Account < ActiveRecord::Base
   has_many :transactions

end

我的帐户表单如下所示(并在数据库中完美保存)

<%= form_for(@account) do |f| %>
  <% if @account.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved:</h2>

      <ul>
      <% @account.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<div class="jumbotron">
  <div class="container">
  <div class="form-group">
    <%= f.label :account_name %><br>
    <%= f.text_field :account_name, class: "form-control" %>
  </div>
  </div>
  <div class="form-group">
    <%= f.label :account_number %><br>
    <%= f.number_field :account_number, class: "form-control" %>
  </div>

    </div>
    <div class="actions">
    <%= f.submit %>
  </form>

<% end %>
</div>
</div>

交易模式

class Transaction < ActiveRecord::Base
    has_many :accounts

end

交易表单看起来像(我使用Pluck方法检索Accounts account_name值并显示所有已创建的帐户):

<div class="form-group">
<%= form_for(@transaction) do |f| %>
  <% if @transaction.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@transaction.errors.count, "error") %> prohibited this transaction from being saved:</h2>

      <ul>
      <% @transaction.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class = "jumbotron">
  <form class="needs-validation" novalidate>


    <div class="col-md-4 mb-3">
    <%= f.label :date %><br>
    <%= f.date_select :date, class:"form-control" %>
    </div>


    <div class="col-md-4 mb-3">
    <%= f.label :reference %><br>
    <%= f.text_field :reference, class:"form-control" %>
    </div>

    <div class="col-md-3 mb-3">
    <%= f.label :account_name %> <br>
     <%= f.select :account_name, Account.pluck(:account_name), {prompt:"Choose Account"}%>
      </div>


  <div class="form-row">
    <div class="col-md-6 mb-3">

    <%= f.label :description %><br>
    <%= f.text_field :description, class:"form-control" %>
   </div>
   </div>

    <div class="col-md-3 mb-3">
    <%= f.label :amount %><br>
    <%= f.text_field :amount, class:"form-control" %></br>
    </div>

   </br></br></br></br></br></br>
   <div class="form-check form-check-inline">
   </br> 
    <%= f.label :payment %>
    <%= f.check_box :payment %> | 

    <%= f.label :receipt %>   
    <%= f.check_box :receipt %>

  </div>



 <div class="actions">
    <%= f.submit %>
<% end %>  

我的交易控制器如下:

class TransactionsController < ApplicationController
  before_action :set_transaction, only: [:show, :edit, :update, :destroy]

  # GET /transactions
  # GET /transactions.json
  def index
    @transactions = Transaction.all
    @balance = 0
    @transactions.each do |transaction|
      if transaction.payment == true
        @balance += transaction.amount
      else 
        @balance -= transaction.amount

    end
   end
  end

  # GET /transactions/1
  # GET /transactions/1.json
  def show
  end

  # GET /transactions/new
  def new
    @transaction = Transaction.new

  end

  # GET /transactions/1/edit
  def edit
  end

  # POST /transactions
  # POST /transactions.json
  def create
    @transaction = Transaction.new(transaction_params)

    respond_to do |format|
      if @transaction.save
        format.html { redirect_to transactions_url, notice: 'Transaction was successfully created.' }
        format.json { render :show, status: :created, location: @transaction }
      else
        format.html { render :new }
        format.json { render json: @transaction.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /transactions/1
  # PATCH/PUT /transactions/1.json
  def update
    respond_to do |format|
      if @transaction.update(transaction_params)
        format.html { redirect_to transactions_url, notice: 'Transaction was successfully updated.' }
        format.json { render :show, status: :ok, location: @transaction }
      else
        format.html { render :edit }
        format.json { render json: @transaction.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /transactions/1
  # DELETE /transactions/1.json
  def destroy
    @transaction.destroy
    respond_to do |format|
      format.html { redirect_to transactions_url, notice: 'Transaction was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_transaction
      @transaction = Transaction.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def transaction_params
      params.require(:transaction).permit(:date, :description, :reference, :account, :amount, :account_id, :payment, :receipt, account_attributes: [:account_name])
    end

end

2 个答案:

答案 0 :(得分:0)

问题似乎在于使用select方法来创建选择标记:

<%= f.select :account_name, Account.pluck(:account_name), {prompt:"Choose Account"}%>

第二个参数应该是一个双元素数组的数组(一个用于select标记中的显示值,另一个用于将作为请求参数发送的值)。

因此,您正在寻找的解决方案看起来像:

<%= f.select :account_name, Account.pluck(:account_name).map { |name| [name, name] }, {prompt:"Choose Account"}%>

或者您可能想要使用options_from_collection_for_select方法:

<%= f.select :account_name, options_from_collection_for_select(Account.all, :name, :name) %>

答案 1 :(得分:0)

您正面临该问题,因为您需要将帐户的id 存储为交易中的参考。

目前您正在保存帐户名称,因此不会预取和保存数据

您可以使用collection_select并将保存id告诉数据库并在选择标记中显示account_name

<强>语法:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
  

:value_method:text_method参数是要调用的方法   在collection的每个成员上。返回值用作value   每个<option>标记的属性和内容。

以下是您示例中的collection_select

<%= f.collection_select(:account_name,Account.all, :id, :account_name, :prompt => 'Choose Account') %>

Here is the documentation for the same