dimanche 19 avril 2015

How to pass data from controller to view with ajax in rails

I posted today similar questions two times


how-to-use-local-or-instance-variable-in-ruby-code-in-coffeescript-in-haml-templ


ajax-in-rails-returns-alway-error-callback


I'm confusing about these issues please let me ask.


I'm using ajax in haml to reload a part of page with data from controller.


Please see my code.


.html.haml



= render 'layouts/partial' #This is partial. I want to reload this part

:coffee
$('input#field').change ->
$.ajax
url: '/posts/gaga'
type: "POST"
dataType: 'text'
error: (jqXHR, textStatus, errorThrown) ->
alert "error"
success: (data, textStatus, jqXHR) ->
alert "success"
$('div.kore').html('#{ j( render( 'layouts/partial' ) )} ');


posts_controller.rb



def gaga
@model = Model.new("blabla")
render :text => @model
end


_partial.html.haml



- if @model
= @model.html_safe
- else
= "No data"


First, I thought data can be get through @model instance variable from controller but it couldn't.


Second, I tryed to get data from ajax response data to view(_partial.html.haml) with jQuery.


Callback and response return correctly as text.


How do I pass data to view from ajax response data, or is there other ways?


Sorry for my sloppy English. I hope someone give me the clue.


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire