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

时间秒杀一切 提交于 2019-12-01 13:14:06

问题


I have an index page with a free form comment field. The Comment field is part of another model that is not associated-- long story, part me, part user.

What I need to figure out is what to use to do that. I ran into a problem with Best In Place (here) and am not sure if that is a resolvable path.

So, does anyone have a tutorial or advice to point me to regarding doing in place editing for an index?


回答1:


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.



来源:https://stackoverflow.com/questions/8914646/rails-3-1-need-to-do-in-place-editing-on-an-index-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!