How to Implement Editing/Formating Text Area in Rails

Keep format text in text area rails?

After I investigated, I found the problem because my current project we have mixed view templates some are erb and some are haml. I have layout in haml layouts/application_new.html.haml and releases/_form.html.erb for resource view template, then it caused this issue. After I switch releases/_form.html.erb to releases/_form.html.haml, then everything works great.

In Rails, how can I allow some html in a text area?

Have you tried this in the views?

 <%= content.html_safe %>

rails form_for edit text area

This is a job for javascript. There are a lot of different resources to check out online.

There are also a lot of prebuilt ones. You can check out (in no particular order):

  • http://ckeditor.com/
  • http://jejacks0n.github.io/mercury/
  • http://www.tinymce.com/

In very basic javascript, you could do something like:

<%= link_to("Bold", '#', onclick: "myFunction(); return false") %>
<script>
function myFunction() {
var textArea = document.getElementById('post_content');
textArea.value += "<b></b>";
}
</script>

Ruby/Rails - Properly Display/Format Text From The Text Area Form Control

Try simpleformat:

<%= simple_format(my_text_from_database) %>

Using Editor in textarea rails

Not sure just what you're seeing, and there's no errors listed in your post so not enough information to go on but your form looks OK.

If you're not opposed to it, you can use the gem for ckeditor. It's just a wrapper around the javascripts to make sure everything works in the asset pipeline without interfering with your form.

https://github.com/tsechingho/ckeditor-rails

3 steps

gem 'ckeditor_rails' Add the gem to the Gemfile

//= require ckeditor-jquery Include the js in application.js

<%= f.text_area :content, :class => 'ckeditor' %> Add the class to your text area input

Rails 3.1 Need to do in place editing on an Index page

What I wound up doing was:

Create a row in the table that was a TextArea and assigned the text area a class:

<td class="textcell" id="<%= crb_agenda.key %>"><%= text_area_tag 'comment', if @pdms_comment.user_comments.nil? == false then @pdms_comment.user_comments end, :rows => 3, :id => "_" + @pdms_comment.jira_key %><%= link_to "[+]", "#", :class => "comment_row" %></td>

[sorry I am having a devil of a time with the formatting for this]

Create a controller for the updating of the field in the DB:

  def comment_push
@jira_key = params[:key]
@comment = params[:comment]
@user_name = params[:name]
@user_pw = params[:pw]

@comment_record = Comment.find_by_jira_key(@jira_key)
@comment_record.update_attribute(:user_comments, @comment)

Comment.add_comment_to_jira_ticket(@user_name, @user_pw, "MCTEST-293",@comment)

respond_to do |format|
format.js
end
end
[note, this required a comment.js.erb file in the views; it was blank. Also, I created a route for it]

Create a jquery function keying off of the class I assigned to the Text Area that passed in the necessary params to the route from the controller...

  $('.comment_row').live("click", function() {
var user_name = $('#user_name').val();
var user_pw = $('#user_pw').val();
var tr = $(this).closest("tr");
var td = $(this).closest("td");
var ta_id = '_' + td.attr("id");
var comment = $('textarea#' + ta_id).val();
$.ajax({
url: '/crbagenda/comments/comment_push',
type: 'GET',
data: 'key=' + td.attr("id") + "&name=" + user_name + "&pw=" + user_pw + "&comment=" + comment
});

And that took care of it.

adding a text area field to devise edit form

Just add the attribute in the form and permit it in the configure_permitted_parameters method.

<div class="field">
<%= f.label :discount_info %><br />
<%= f.text_area :discount_info %>
</div>

And in the controller

def configure_permitted_parameters
#other code
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :discount_info) }
end


Related Topics



Leave a reply



Submit