4

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 %>
12
  • So the error from rails said that rating wasn't a number? Commented Jun 17, 2015 at 11:23
  • Yes, the exception's "parameters" part said that Commented Jun 17, 2015 at 11:24
  • Pass only_integer: true inside hash. Commented Jun 17, 2015 at 11:25
  • can you restart rails server.. ? Blind guess I did.. :p Commented Jun 17, 2015 at 11:26
  • I did that, same results. You are saying the validation hash, right Arslan? Commented Jun 17, 2015 at 11:29

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

oh, @comment doesn't anymore
0

I think you need to setup the form properly, I think the validation will work fine if the form posts as it should:

comments_controller.rb

def new
  @comment = Comment.new
end

def create
  @comment = Comment.new(comment_params)
  raise "It exploded into pieces!" unless @comment.save
end

private

def comment_params
  params.require(:comment).permit(:rating, :body, :name, :game_id)
end

There we are changing the strong params to require the new comment key in params (which will be the result of the second change to the view below). I also moved this into a private function to clean this up.

So then in your view:

<% if @comments.count < 10 %>
    <%= form_for(@comment) 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 %>

This uses the form_for tag with the model instance which should, I think, solve this issue.

7 Comments

I just updated the answer with a bit more detail on the changes I made.
undefined method `comments_path' for #<#<Class:0x007f9a46f0fae0>:0x007f9a46f0eed8> There is only #new in comments.
@avaragecoder Haven't you defined routes for comments?
Yes you need to add 'resources :comments, only: [:new, :create]' (no quotes) to your routes file. You can then add more methods to the only part as and when needed.
you see, my new function doesnt exist, comments are created in games#show, which goes to comments#create
|
0

try this in the comment_controller.rb

@rating = params[:comment][:rating].to_i

to make every input of the ratings to be converted as integer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.