ruby-on-rails - 在 Ruby on Rails 中通过 ajax 呈现局部 View

标签 ruby-on-rails ajax render single-page-application rails-routing

目前我正在尝试让我的 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/

相关文章:

c++ - OpenGL 渲染错误

ruby-on-rails - Heroku 不加载预编译的 Assets

javascript - 这个 demo_get.asp 是什么?

javascript - 避免两个 php 请愿同时发生

jquery - 如果添加表单标签,Ajax 无法工作

java - 如何在Opengl ES中获得一个像素的透明颜色?

user-interface - 为什么 intellij 中不同主题的字体看起来如此不同?

ruby-on-rails - 如何覆盖lib目录中的rails引擎文件?

ruby-on-rails - will_paginate JSON 支持?

ruby-on-rails - 在 Rails 应用程序中处理动态 css 的最佳方式