如何在rails中使用ajax将数据从控制器传递到视图
I posted today similar questions two times
我今天发了两次类似的问题
how-to-use-local-or-instance-variable-in-ruby-code-in-coffeescript-in-haml-templ
how-to-use-local-or-instance-variable-in-ruby-code-in-coffeescript-in-haml-templ
ajax-in-rails-returns-alway-error-callback
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.
我在haml中使用ajax重新加载来自controller的数据。
Please see my code.
请参阅我的代码。
.html.haml
.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
posts_controller.rb
def gaga
@model = Model.new("blabla")
render :text => @model
end
_partial.html.haml
_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.
首先,我认为数据可以通过控制器的@model实例变量获得,但它不能。
Second, I tryed to get data from ajax response data to view(_partial.html.haml) with jQuery.
其次,我尝试使用jQuery从ajax响应数据获取数据到view(_partial.html.haml)。
Callback and response return correctly as text.
回调和响应以文本形式正确返回。
How do I pass data to view from ajax response data, or is there other ways?
如何从ajax响应数据传递数据到视图,还是有其他方法?
Sorry for my sloppy English. I hope someone give me the clue.
对不起,我的英语太马虎了。我希望有人能给我一些线索。
Thanks in advance!
提前谢谢!
1 个解决方案
#1
2
You can set the ajax request to /posts/gaga.json
and in your controller's gaga method:
可以将ajax请求设置为/posts/gaga。json和你的控制器的gaga方法:
respond_to do |format|
format.html
format.json { render :json => @model }
end
You may want to prepare gaga.html.haml
view file if you intend to also access /posts/gaga
not via ajax, or just remove format.html
if you only intend to respond in json.
您可能需要准备gaga.html。haml视图文件如果你也想访问/post /gaga而不是通过ajax,或者只是删除格式。如果您只想用json响应,请使用html。
To clarify, I see you want to reload the partial after ajax request. So rather than:
为了澄清,我看到您希望在ajax请求后重新加载部分。而不是:
$('div.kore').html('#{ j( render( 'layouts/partial' ) )} ');
you can do something like this:
你可以这样做:
# inside gaga methods
format.json do
render(json: {
view: render_to_string(partial: 'layouts/partial', layout: false)
})
end
Now inside your view:
现在在你的观点:
= render 'layouts/partial' #This is partial. I want to reload this part
:coffee
$('input#field').change ->
$.ajax
url: '/posts/gaga.json'
type: "POST"
error: (jqXHR, textStatus, errorThrown) ->
alert "error"
success: (data, textStatus, jqXHR) ->
alert "success"
$('div.kore').html(data.view)
更多相关文章
- 如果外部应用程序更改了持久模型(服务器数据库),AngularJS可以自
- Javascript来自数据库的图像上的图像映射
- python数据类型二(列表和元组)
- 将敏感数据存储在数据库中,建议
- sklearn进行数据预处理-归一化/标准化/正则化
- 使用特定顺序的ID列表从Django数据库中获取记录
- 检查 NaN 数据值 (C/C++/Python 实现)
- django的视图和URL配置
- 【python网络爬虫三】爬取动态数据及数据入库