Rails 4 Integer number field returns string
It's a strong params thing. Permit :comments, then the attributes
params.require(:comments).permit(:rating, :body, :name, :game_id)
and, use form_for @comment, not comment_path
ooransoy
Updated on June 04, 2022Comments
-
ooransoy almost 2 years
When I enter something into a
number_field
and post the form to the controller, Rails can't save because i have a numericality validation:validates :rating, numericality: { greater_than: 0, less_than: 6 }
I debugged the controller by this piece of code:
raise "It exploded into pieces!" unless @comment.save
The exception I used for debugging said that my
:rating
was a string instead of an integer. Before this, i rendered the json errors for@comment
and that said that:rating
was not a number.These are very useful to spot the problem, but I can't find any solutions to fix the problem. I checked the database schema and it says that
:rating
should be an integer, as in:t.integer "rating"
I don't know what to do at this point. Can somebody help me? Thank you in advance.
P.S. I use
number_field
.P.P.S. In my controller:
def ccreate @comment = Comment.new(params.permit(:rating, :body, :name, :game_id)) raise "It exploded into pieces!" unless @comment.save end
In my view:
<% if @comments.count < 10 %> <%= form_for(comment_path) do |f| %> <div class="field"> <%= f.label :rating %><br> <%= f.number_field :rating %> </div> <div class="field"> <%= f.label :body %><br> <%= f.text_area :body %> </div> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> <% end %>