Ruby w/ Sinatra:什么是.js的意思?从rails erb吗?

时间:2021-05-28 10:59:42

.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.

. js。erb是很好的,因为你可以使用它们来替换页面的一部分,而不需要离开当前页面,这给站点/应用程序提供了一种干净的、未分割的感觉。

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

在西纳特拉有什么办法使用它们吗?或一个等价的吗?

3 个解决方案

#1


2  

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.

根据您的描述,我猜您的愿望是拥有页面可编辑的部分并通过AJAX替换。如果这是错误的,请澄清。

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.

在我的Sinatra应用程序中,我包括(我自己的)AJAXFetch jQuery库并编写代码,如下所示。这使我在最初呈现页面时和通过AJAX编辑页面时都可以使用部分,以达到最大的干燥。AJAXFetch库仅通过标记处理所有的AJAX获取/交换,而不需要在使用它的页面上编写定制的JS。

helpers/partials.rb

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

routes/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

#2


3  

Just add .js to the end of the symbol you're passing erb(). A la (to call mypage.js.erb):

只需在传递erb()的符号末尾添加.js。A la(称为mypage.js.erb):

erb "mypage.js".to_sym

Dirty, but it works.

脏,但它的工作原理。

#3


0  

sinatra really isn't meant to be a full stack framework. Its supposed to get you on the road very quickly. You could use an erb separately and then load into your sinatra code.

sinatra并不是一个完整的栈框架。它会让你很快上路。您可以单独使用erb,然后加载到sinatra代码中。

#1


2  

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.

根据您的描述,我猜您的愿望是拥有页面可编辑的部分并通过AJAX替换。如果这是错误的,请澄清。

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.

在我的Sinatra应用程序中,我包括(我自己的)AJAXFetch jQuery库并编写代码,如下所示。这使我在最初呈现页面时和通过AJAX编辑页面时都可以使用部分,以达到最大的干燥。AJAXFetch库仅通过标记处理所有的AJAX获取/交换,而不需要在使用它的页面上编写定制的JS。

helpers/partials.rb

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

routes/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

#2


3  

Just add .js to the end of the symbol you're passing erb(). A la (to call mypage.js.erb):

只需在传递erb()的符号末尾添加.js。A la(称为mypage.js.erb):

erb "mypage.js".to_sym

Dirty, but it works.

脏,但它的工作原理。

#3


0  

sinatra really isn't meant to be a full stack framework. Its supposed to get you on the road very quickly. You could use an erb separately and then load into your sinatra code.

sinatra并不是一个完整的栈框架。它会让你很快上路。您可以单独使用erb,然后加载到sinatra代码中。