目前我正在尝试让我的 Rails 应用程序单页显示,但我在渲染部分时遇到了一些问题。现在这是我的 Controller 代码:
class WelcomeController < ApplicationController
def create
@welcome = WelcomeController.new
render :partial => 'enjoy'
end
def index
if params[:id] == 'enjoy'
@partial_view = 'enjoy'
elsif params[:id] == 'about'
@partial_view = 'about'
else
@partial_view = 'main'
end
end
end
所以这个 Controller 链接到我的 View ,为了改变页面,我使用了索引操作中的代码,但现在我试图在创建操作中使用这个东西。我试图调用创建操作的 index.html.slim 代码是:
div id="content"
= form_tag(:url => {:action => 'create'},
:update => "content", :position => :bottom,
:html => {:id => 'subject_form'},
:remote => true)
= submit_tag 'Add'
所以在按钮上单击“添加”,我期待来自 _enjoy.slim.html 的内容,这是我部分进入 ID 为“content”的 div,但是当单击按钮和 _enjoy.html 时。 slim 呈现为加载到整个 index.html.slim 页面的顶部...所以我被困住了,我怎样才能让它加载到特定的 div 中?
非常感谢任何对此问题有答案的人{:
最佳答案
Controller :
class WelcomeController < ApplicationController
def create
@welcome = WelcomeController.new
# Remove the render
end
def index
# No change
end
end
查看:
div id="content"
= form_tag({:action => 'create'}, :id => 'subject_form', :remote => true) do
= submit_tag 'Add'
创建 welcome/create.js.erb 并向其中添加以下行:
$('#content').html("<%= j render(:partial => 'welcome/enjoy') %>")
关于ruby-on-rails - 在 Ruby on Rails 中通过 ajax 呈现局部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118275/