ruby-on-rails Rails枚举“0”不是有效的incomeType

wvyml7n5  于 2023-08-08  发布在  Ruby
关注(0)|答案(2)|浏览(120)

我有一个Ruby on Rails应用程序,可以处理用户的收入,决定它是津贴还是收入,并应用适当的税率。我已经包含了一些enums来完成下面列出的基本功能。
当我从默认更新时,我遇到了问题“1”不是有效的incomeType。
下面你可以看到设置,包括模型,控制器和窗体。

型号:

class Income < ApplicationRecord
  enum incomeType: {income: 0, allowance: 1 }
  enum taxed: {yes: 0, no: 1 }

  belongs_to :user
end

字符串

控制器:

class IncomesController < ApplicationController
  before_action :set_income, only: [:show, :edit, :update, :destroy]

  # GET /incomes
  # GET /incomes.json
  def index
    @incomes = current_user.incomes.all
  end

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

  # GET /incomes/new
  def new
    @income = current_user.incomes.build
  end

  # GET /incomes/1/edit
  def edit
  end

  # POST /incomes
  # POST /incomes.json
  def create
    @income = current_user.incomes.new(income_params)

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

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

  # DELETE /incomes/1
  # DELETE /incomes/1.json
  def destroy
    @income.destroy
    respond_to do |format|
      format.html { redirect_to incomes_url, notice: 'Income was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def income_params
      params.require(:income).permit(:amount, :frequency, :user_id, :incomeType, :country, :taxed)
    end
end

形式:

<%= form_with(model: income, local: true) do |form| %>
  <% if income.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(income.errors.count, "error") %> prohibited this income from being saved:</h2>

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

  <div class="field">
    <%= form.label :amount %>
    <%= form.text_field :amount, id: :income_amount %>
  </div>

  <div class="field">
    <%= form.label :frequency %>
    <%= form.select :frequency, options_for_select([['Weekly', '52'], ['Fortnightly', '26'], ['Monthly', '12'], ['Bi-Monthly', '6'], ['Annually', '1']]), id: :income_frequency %>
  </div>
  <div class="field">
    <%= form.label :incomeType %>
    <%= form.select :incomeType, options_for_select([['Income', '0'], ['Allowance', '1']]), id: :incomeType %>
  </div>
  <div class="field">
    <%= form.label :taxed %>
    <%= form.select :taxed, options_for_select([['Yes', '0'], ['No', '1']]), id: :taxed %>ra
  </div>

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


希望你能给我指明正确的方向。

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"HUTd5Bav9eAfWPTFFyqeD69aL4mIgZodIuL1+9eL0zIhN+SjDwAMcD7AzKEuhm6az4iALBSrDUXd/1vVfN77SQ==",
 "income"=>{"amount"=>"102000.0", "frequency"=>"1", "incomeType"=>"0", "taxed"=>"1"},
 "commit"=>"Update Income",
 "id"=>"4f9fc439-4578-487e-bc5d-02cf0cd9aaa3"}


先谢谢你的帮助

bfnvny8b

bfnvny8b1#

是的,这是因为枚举需要的是key而不是value,而你发送的是value而不是key,所以在你的例子中,枚举试图找到“0”作为一个不存在的key。所以只要稍微改变一下你的形态

<%= form.select :incomeType, options_for_select([['Income', 'income'], ['Allowance', 'allowance']]), id: :incomeType %>

字符串
PS:也要换一种形式来征税。希望这能有所帮助

nuypyhwy

nuypyhwy2#

@Manishh的answer很棒。只是想展示一些在select表单中显示枚举选项的其他方法:

<div>
  <%= form.label :status %>
  <%= form.select :status, Post.statuses.keys %>
</div>

字符串
和/或

<div>
  <%= form.label :status %>
  <%= form.select :status, Post.statuses.keys.map{ |key| [key.humanize, key] }, selected: @post.status || :draft %>
</div>


这两个例子都来自this源代码。

相关问题