roguerat
roguerat

Reputation: 217

Rails: number_field isn't showing up when outputting a form?

I have a basic form at the moment, I inserted a it has a title, body and a number_field. All three of them are working it seems, except when I go to my index to view the posts only the title and body show up, no number.

the controller: 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 %>

and my index.html.erb

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

Upvotes: 0

Views: 91

Answers (1)

Dabrorius
Dabrorius

Reputation: 1039

You forgot to permit the price in your strong params helper method, try this:

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

Your price value was never saved, so that's why it's not showing up. Try saving it again after these changes and it should work!

You can read more about strong parameters and how to use them properly here.

Upvotes: 1

Related Questions