Ruby w/Sinatra:相当于 rails 中的 .js.erb 吗? [英] Ruby w/ Sinatra: what is the equivalent of a .js.erb from rails?

查看:31
本文介绍了Ruby w/Sinatra:相当于 rails 中的 .js.erb 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.js.erb 很不错,因为您可以使用它们来替换页面的某些部分,而无需离开当前页面,这让网站/应用程序看起来更干净、更完整.

.js.erb's are nice, because you can use them to replace parts of a page without having to leave the current page, which gives a cleaner and unchopped up feel to the site / app.

有没有办法在 sinatra 中使用它们?或等价物?

Is there a way to use them in sinatra? or an equivalent?

推荐答案

根据您的描述,我猜您希望通过 AJAX 编辑和替换页面的某些部分.如果这是错误的,请澄清.

Based on your description, I'm guessing that your desire is to have portions of a page editable and replaced via AJAX. If this is wrong, please clarify.

我通过包含(我自己的)AJAXFetch jQuery 库 并编写代码,在我的 Sinatra 应用程序中执行此操作如下所示.这让我在最初渲染页面和通过 AJAX 编辑时都使用部分,以获得最大的干燥度.AJAXFetch 库仅通过标记处理所有 AJAX 获取/交换,无需在使用它的页面上编写自定义 JS.

I do this in my Sinatra apps by including (my own) AJAXFetch jQuery library and writing code as shown below. This lets me use the partial both when rendering the page initially as well as when editing via AJAX, for maximum DRYness. The AJAXFetch library handles all AJAX fetch/swap through markup alone, without needing to write custom JS on the pages that use it.

require 'sinatra/base'
module Sinatra
  module PartialPartials
    ENV_PATHS = %w[ REQUEST_PATH PATH_INFO REQUEST_URI ] 
    def spoof_request( uri, headers=nil ) 
      new_env = env.dup 
      ENV_PATHS.each{ |k| new_env[k] = uri.to_s } 
      new_env.merge!(headers) if headers
      call( new_env ).last.join 
    end
    def partial( page, variables={} )
      haml page, {layout:false}, variables
    end
  end
  helpers PartialPartials
end

路由/bug.rb

get '/bug/:bug_id' do
  if @bug = Bug[params[:bug_id]]
    # ...
    haml :bug
  end
end

# Generate routes for each known partial
partials = %w[ bugdescription bughistory bugtitle fixer
               pain project relatedbugs status tags version votes ]
partials.each do |part|
  [ part, "#{part}_edit" ].each do |name|
    get "/partial/#{name}/:bug_id" do
      id = params[:bug_id]
      login_required
      halt 404, "(no bug ##{id})" unless @bug = Bug[id]
      partial :"_#{name}"
    end
  end
end

post "/update_bug/:partial" do
  id = params[:bug_id]
  unless params['cancel']=='cancel'
    # (update the bug based on fields)
    @bug.save
  end
  spoof_request "/partial/#{params[:partial]}/#{id}", 'REQUEST_METHOD'=>'GET'
end

views/bug.haml

#main
  #bug.section
    = partial :_bugtitle
    .section-body
      = partial :_bugdescription
   <!-- many more partials used -->

views/_bugtitle.haml

%h1.ajaxfetch-andswap.editable(href="/partial/bugtitle_edit/#{@bug.pk}")= title

views/_bugtitle_edit.haml

%form.ajaxfetch-andswap(method='post' action='/update_bug/bugtitle')
  %input(type="hidden" name="bug_id" value="#{@bug.id}")
  %h1
    %input(type="text" name="name" value="#{h @bug.name}")
    %span.edit-buttons
      %button(type="submit") update
      %button(type="submit" name="cancel" value="cancel") cancel

这篇关于Ruby w/Sinatra:相当于 rails 中的 .js.erb 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆