thank_you
thank_you

Reputation: 11107

Textarea Not Sending Value through Form

So I have a form in my view..

  form_for(ActivityComment.new, remote: true, url: "/activity_comments/create", class: "col-md-6") do |f|
    f.hidden_field :klass_name, value: activity.klass
    f.hidden_field :klass_id, value: activity.id
    f.text_area :comment, class: "form-control"
    f.submit "Submit", class: "btn btn-success"
  end

The html is printed out as...

<form accept-charset="UTF-8" action="/activity_comments/create" class="new_activity_comment" data-remote="true" id="new_activity_comment" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="✓">
  </div>
  <input id="activity_comment_klass_name" name="activity_comment[klass_name]" type="hidden" value="Friend">
  <input id="activity_comment_klass_id" name="activity_comment[klass_id]" type="hidden" value="3">
  <textarea class="form-control" id="activity_comment_comment" name="activity_comment[comment]"></textarea>
  <input class="btn btn-success" name="commit" type="submit" value="Submit">
</form>

However the params I receive in the controller is...

Parameters: {"utf8"=>"✓", "activity_comment"=>{"klass_name"=>"Friend", "klass_id"=>"3", "comment"=>""}, "commit"=>"Submit"}

The activity comment model

# == Schema Information
#
# Table name: activity_comments
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  klass_name :string(255)
#  klass_id   :integer
#  comment    :text
#  created_at :datetime
#  updated_at :datetime

class ActivityComment < ActiveRecord::Base
  belongs_to :user
end

The comment attribute is returning empty no matter what. I'm to believe that it has something to do with the textarea as using a plain input field made it work. What's wrong and how do I fix it?

Upvotes: 6

Views: 2232

Answers (3)

Beat Richartz
Beat Richartz

Reputation: 9622

It'll be hard to come up with a solution since this seems to be a very located problem. So here are just some steps I'd recommend to take in order to nail it down, rather than a shorthand solution:

  1. Check the output of $('#new_activity_comment').serializeArray() in the browser console when you are on the actual page with the form in it.

    This is what the js handling the data-remote="true" forms in jquery-rails does to serialize your form data before it sends it to the server.

    The resulting Javascript Array should have an object with name "activity_comment[comment]" and value "Your comment" in it.

    If it does not, your error comes from somewhere in your Javascript or HTML. Where is difficult to say unless you post the JS sources, your HTML Doctype etc.

    EDIT: 1.5. Remove the "form-control" class from the textarea and see if it solves your problem. If it does not, try to rip out all the JS coming from Bootstrap and check if your forms are doing ok now. If they are, the problem lies with Bootstrap.

  2. Check your Networks tab in the developer tools when you send the request: Your headers should have FormData in them and there you should see activity_comment[comment] and your textarea value. If you don't, chances are you're operating on a broken build of jQuery or something with your browser is wrong / you're using a broken build of that, though that is very very unlikely unless you're on some Canary / Alpha Build channel for Chromium or something else.

  3. If your Javascript and Browser is sending the form ok and you still get no textarea value in your rails controller, log the contents of the raw rack parameters like that to your rails log:

    Rails.logger.info '###' #makes it easier to find your params
    Rails.logger.info URI.decode(request.env["rack.request.form_vars"])
    

    Now, if Rack has the right form value, but rails does not, there are 2 possibilities:

    1. Either there is something messed up in your app with strong parameters, meaning you will have to check for any params.require or params.permit in your ActivityCommentsController or any Controller in its ancestors
    2. There's a bug somewhere in the Rails Rack/ActionController interface, though that is also highly unlikely. Try to update rails to the latest patch version in that case or try to search for ActionController bugs in the Rails issues on github.

Upvotes: 1

Joe Saunders
Joe Saunders

Reputation: 798

Maybe try adding attr_accessable :comment to your model

Upvotes: 0

pguardiario
pguardiario

Reputation: 55012

Are you sure that you understand what form_for remote :true does? It turns your form into an ajax loader. So if that's acting up, then some javascript is to blame. Look at your javascript console in chrome for clues.

Upvotes: 0

Related Questions