9

I have an idea and a problem I can't seem to find answer or solution to. Some info if required later or helpful:

  • Rails version 3.2.9
  • best_in_place (git://github.com/straydogstudio/best_in_place.git)
  • Ruby 1.9.3p327

Ok, so i have settings page where i can update individual setting by editing them with use of best_in_place gem. Works fine. Happy with that.

Some of the settings are interconnected, meaning, i have to sum or subtract them. As a helpful tip for the user, in my view, right beside the in place form for that settings there is also a calculated value.

Now, of course, I would like to see this value be update along with the attribute itself. I can't find a way to do that.

If i do it with the :data => it works, but i get the old and not the new value, so my view is always "1 step behind".

i have also tried with update.js.erb and _test.html.erb partial, but javascript file doesn't work. It is like it doesn't exist. I have double, triple checked where to put it and it is OK (app/views/controller/_partial.html.erb)

So. Pretty straightforward question would be; how can i access an updated value and use it back in view to update calculations. I personally think I should go with the partial and js file - but I have no clue why JS file isn't picked up. Any idea on that?

If there are any other options, i would more than appreciate the hint.

Thanks!

--EDIT (code added)

view:

<td>
  <%= best_in_place @s,:pay_after_bonus, :display_with => :number_to_percentage, :type => :input, :nil => "Klikni tu za spremembo!", :cancel_button=> "Prekliči" %>
  Cena: <span id="test"><%= number_to_currency(@s.family_member_price - ((@s.pay_after_bonus * @s.family_member_price)/100)) %></span>
</td>

settings_controller.rb:

def update
  @s = Setting.find(params[:id])

  respond_to do |format|
    @s.update_attributes params[:setting]
   @s.create_activity :update, :params => { :setting => params[:setting].keys.first }, owner: current_user
   format.json { respond_with_bip(@s) }
   format.js
 end
end

update.js.erb:

$( "#test" ).html( "<%= escape_javascript( render( :partial => "update") ) %>" );

_update.html.erb:

<strong>Test</strong>

-- EDIT 2:

OK, apparently it is possible to do something like I want this way:

$('.best_in_place[data-attribute="model_attribute"]').bind(
"ajax:success", function(event, data) {
    // function that will update whatever
});

in combination with

// respond to json request with
render :json => {"model" => @model}

&

"ajax:success", function(event, data) {
var result = $.parseJSON(data);
// from here the result var will be accessible with all the data returned by the controller.
// result.model is your object - use result.model.attribute  to get specific values...
}

But here it ends for me. I don't know how to use render :json => {"model" => @model} in my case, as it has to be done in combination with format.json { respond_with_bip(@s) }.

Where do I put render in controller? Currently I get 500 internal server errors trying to do this as a response.

I have found this solution here.

Thanks!!

4

3 回答 3

1

在 ajax 回调中,您可以发出另一个请求以获取所需的部分:

$('.best_in_place.myclass').bind("ajax:success", function () {
  $.getScript("http://www.someurl.com");
});

然后在动作中渲染一个 JS 文件(例如:show.js.erb),在其中将目标元素替换为渲染结果:

$("#div-<%= @div.id %>").replaceWith("<%= escape_javascript(render(partial: 'some/partial', :layout => false)) %>");
于 2014-08-11T20:49:15.880 回答
0

您可以使用 jQuery 为刚刚更改的最佳元素解析 dom,并从那里获取更新的值。例如,你有这个代码(haml)

= best_in_place @user, :name, :classes => 'name-edit'

然后你的回调看起来像这样 (coffeescript)

$('.name-edit').on 'ajax:success', (event, data, status, xhr) ->
  newValue = $('.name-edit').html # could also use .attr() here
  $('.some-other-widget').html(newValue) # again could also set .attr here

您甚至可以跳过使用 jQuery 查找新值。在回调处理程序的上下文中,“this”表示调用的元素,所以你可以这样做

$('.name-edit').on 'ajax:success', (event, data, status, xhr) ->
  $('.some-other-widget').html this.innerHTML

并以这种方式将新值传递给另一个小部件。我的猜测是 ajax 处理程序返回的事件也有 currentTarget,这也是触发 ajax 请求的小部件。我对所有这一切的唯一担心是您的成功处理程序以某种方式击败了最佳处理程序,并且您在更新之前获得了小部件。在我的测试中,这从未发生过。

于 2013-05-02T22:07:03.693 回答
-1

我刚刚回答了这样一个问题:

在这里回答

但不仅仅是插入您需要的值,而是使用您需要的总和。

于 2013-04-11T17:09:06.693 回答