Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 667
thank_you Avatar asked Dec 07 '13 00:12

thank_you


People also ask

Can textarea be used in form?

The <textarea> element is often used in a form, to collect user inputs like comments or reviews. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier).

Can textarea be used outside form?

Yes you can use textarea tags outside of a form and they will display and allow text to be inserted and edited, but not being tied to a form their uses will likely be limited.

Does textarea support value attribute?

<textarea> does not support the value attribute.

How do I display HTML content in textarea?

Complete HTML/CSS Course 2022Use the <textarea> tag to show a text area. The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows. Specifies that on page load the text area should automatically get focus.


1 Answers

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.
like image 113
Beat Richartz Avatar answered Oct 24 '22 15:10

Beat Richartz