【问题标题】:Data not getting stored in table rails数据未存储在表导轨中
【发布时间】:2018-05-24 02:50:26
【问题描述】:

我已经生成了一个迁移,通过使用 rails generate migration add_status_to_fundings 在资金表中添加一个状态字段。迁移文件被创建并在其中,我添加了 add_column :fundings, :status, :string 然后 rails db:migrate 在架构 status 中被添加到资金表中。但无论我在状态中输入什么数据,它都不会存储在资金表中。 show.html.erb 文件中的数据也不显示。请帮忙。

_form.html.erb

<div class = "form-group">
        <div class="control-label col-sm-2">
          <%= form.label :status %>
        </div>
        <div class="col-sm-8">
          <%= form.text_field :status %>
        </div>
      </div> 
      <div class="form-group">
        <div class="col-sm-10">
          <%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
</div>
</div>

show.html.erb

<p><strong>Status: </strong><%= @funding.status %></p><br>

迁移文件

class AddStatusToFundings < ActiveRecord::Migration[5.2]
  def change
    add_column :fundings, :status, :string
  end
end

轨道控制台

<Funding id: 1, describe_activity: "10 players in each team", type_of_activity: "Cricket", season: "Fall", activity_details: "best outdoor games ", name_of_organisation: "BCCI", activity_start_date: "2018-12-12", number_of_weeks: "6", days_per_week: "6", hours_per_day: "6", program_registration_cost: 100, family_contribution: "10", other_funds: 20, other_fund_provider: "Arni", amount_requested: 70, created_at: "2018-05-23 17:56:03", updated_at: "2018-05-23 17:56:03", child_id: 1, status: nil>

【问题讨论】:

  • 您是否在控制器的允许参数中包含:status
  • 对于状态字段,我想将其设为下拉列表并希望用户等待它,并且只有管理员可以将其更改为已批准或已拒绝。

标签: ruby-on-rails ruby


【解决方案1】:

看来你的问题不在于表本身。您只是忘记将 :status 添加到允许的参数中。

也许你的控制器中有类似的东西

private
  def funding_params
    params.require(:funding).permit(:describe_activity, :type_of_activity..., :status)
  end

只需添加 :status 即可

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多