Rails:输出表单时没有显示number_field?

时间:2015-02-27 18:11:13

标签: ruby-on-rails ruby forms

此刻我有一个基本形式,我插入了一个标题,正文和数字字段。看起来他们三个都在工作,除非我去我的索引查看帖子只显示标题和正文,没有数字。

控制器:     class AuctionsController< ApplicationController中

  def index
    @auctions = Auction.all
  end

  def new
    @auctions = Auction.new
  end

  def create
    @auctions = Auction.new(auction_params)
    if @auctions.save
      redirect_to auctions_path
    else
      render "new"
    end
  end


  def destroy
    @auctions = Auction.find(params[:id])
    @auctions.destroy
    redirect_to auctions_path
  end


  private
  def auction_params
    params.require(:auction).permit(:title, :details)
  end

end

_form.html.erb

<%= form_for Auction.new do |f| %>
  <%= f.text_field :title %> <br>
  <%= f.text_area :details %> <br>
  <%= f.number_field :price %> <br>
  <%= f.submit "Submit" %>
<% end %>

和我的index.html.erb

<% @auctions.each do |auction| %>
  <%= auction.title %> <br>
  <%= auction.details %> <br>
  <%= auction.price %> <br>
  <%= link_to "Delete", auction, :method => :delete %> 
<% end %>

1 个答案:

答案 0 :(得分:1)

你忘了在强大的params辅助方法中允许价格,试试这个:

  def auction_params
    params.require(:auction).permit(:title, :details, :price)
  end

您的价格价值从未保存过,所以这就是它没有出现的原因。 尝试在这些更改后再次保存它,它应该可以正常工作!

您可以阅读有关强参数以及如何正确使用它们的更多信息here