Ruby w/ Sinatra: what is the equivalent of a .js.erb from rails?

六眼飞鱼酱① 提交于 2019-12-20 17:24:49

问题


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

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


回答1:


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.

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.

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:


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

erb "mypage.js".to_sym

Dirty, but it works.




回答3:


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.



来源:https://stackoverflow.com/questions/4857374/ruby-w-sinatra-what-is-the-equivalent-of-a-js-erb-from-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!