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!!

Mitja Čebokli
  • 385
  • 4
  • 14

3 Answers3

1

In the ajax callback you can make another request to get the partial you want:

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

Then in the action you render a JS file (eg: show.js.erb), where you replace the target element with the results of the render:

$("#div-<%= @div.id %>").replaceWith("<%= escape_javascript(render(partial: 'some/partial', :layout => false)) %>");
apeniche
  • 659
  • 3
  • 8
0

You can use jQuery to parse the dom for the element that best in place just changed, and get the updated value from there. For example, of you have this code (haml)

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

Then your callback would look like this (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

You can even skip looking up the new value with jQuery. In the context of the callback handler, 'this' represents the element the call was made from, so you could just do

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

and get the new value to the other widget that way. My guess is the event returned by the ajax handler also has currentTarget, which again would be the widget that trigged the ajax request. My only worry on all this would be that your success handler somehow beats the best in place handler, and you get the widget before it's updated. In my testing that hasn't ever happened.

tehfoo
  • 328
  • 2
  • 7
-1

I just answered a question like that:

Answer here

but instead of just inserting the value you need to use the sum that you need.

Community
  • 1
  • 1
Bernardo Mendes
  • 1,220
  • 9
  • 15