【问题标题】:not correctly display results in a textarea Ruby + Sinatra + Haml无法在文本区域 Ruby + Sinatra + Haml 中正确显示结果
【发布时间】:2013-10-15 19:00:27
【问题描述】:
#encoding:utf-8
#! /usr/local/bin/ruby

require "sinatra"
require "haml"

Encoding.default_internal = Encoding.default_external = "UTF-8"

get "/*" do
  haml :index
end

post '/' do
  @track_in = params[:track_in]
  @track_out = out(@track_in)
  haml :index
end

def out(trackin)
  if trackin != nil
    track = trackin.lines.to_a
    for i in 0 .. track.size
      if track[i] != nil
        tr = track[i].chomp.split(/\s\t/)
        n,r,k = tr[0],tr[1],tr[2]
        t = "#{n}.#{k}(#{r})" if k != nil
        t = "#{n}.#{r}" if k == nil or track[i] == /([0-9]{2})\s\t(.+)\r/
        puts "#{t}\n"
      end
    end
  end
end

__END__

@@ index

!!!
%html
  %head
    %meta#_moz_html_fragment/
    %title
  %body
    #main_wrapper
      #page
        #editing_header
        .clear
        #preview{style: "border: 5px solid rgb(204, 204, 204); margin: 1em 0pt; padding: 5px; background: rgb(255, 255, 255) none repeat scroll 0% 50%; display: none; width: 75%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"}
        %form#form{action: "", method: "post"}
          %table{border: "0", cellpadding: "0", cellspacing: "0", style: "width: 0:px;"}
            %thead
            %tbody
            %input{name: "Submit", type: "submit"}/
          %textarea#track_in{cols: "100", rows: "15", name: "track_in"}= @track_in
          %textarea#track_out{cols: "100", rows: "15", name: "track_out"}= @track_out

输出 http://i.stack.imgur.com/nhplC.jpg 期望的结果 http://i.stack.imgur.com/T0xtJ.jpg

转换曲目列表。将点放在正确的位置,括号内的文本并按特定顺序

请帮帮我

【问题讨论】:

标签: ruby sinatra textarea haml return-value


【解决方案1】:

您没有从 out 方法返回正确的结果。试试这个:

def out(trackin)
  res = []
  if trackin != nil
    track = trackin.lines.to_a
    for i in 0 .. track.size
      if track[i] != nil
        tr = track[i].chomp.split(/\s\t/)
        n,r,k = tr[0],tr[1],tr[2]
        t = "#{n}.#{k}(#{r})" if k != nil
        t = "#{n}.#{r}" if k == nil or track[i] == /([0-9]{2})\s\t(.+)\r/
#       puts "#{t}\n"
        res << "#{t}\n"
      end
    end
  end
  res.join
end

现在您的out 返回所需结果的字符串。方法将字符串收集到数组res 中,最后将所有行连接成一个字符串,您应该在文本框中看到该字符串。

【讨论】:

  • Спасибо за помощь,надеюсь в дальнейшемя буду учиться внимательнее
  • 没问题。祝你好运;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多