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

前端 未结 3 1456
天命终不由人
天命终不由人 2021-02-06 17:52

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

相关标签:
3条回答
  • 2021-02-06 18:26

    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.

    0 讨论(0)
  • 2021-02-06 18:29

    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.

    0 讨论(0)
  • 2021-02-06 18:46

    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
    
    0 讨论(0)
提交回复
热议问题