简体   繁体   English

Ruby-从控制器传递字符串以查看空格

[英]Ruby - Pass a string from controller to view with spaces

I am attempting to send a string variable from the controller to the view using an instance variable. 我正在尝试使用实例变量将字符串变量从控制器发送到视图。 The issue is that when the view is rendered the string variable that is sent is stripped of all spaces that were in the original string. 问题在于,渲染视图时,将发送的字符串变量删除原始字符串中的所有空格。 Is there some special property of ruby strings or how rails handles them that I'm missing? 红宝石弦是否有一些特殊的属性,或者我不知道它是如何用铁轨处理的?

Ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux] Ruby 2.0.0p481(2014-05-08修订版45883)[x86_64-linux]
Rails 4.0.5 Rails 4.0.5

Controller 控制者

def stringsub
  forminput = params['mystring']
  spaceinput = String.new(forminput)
  puts "Displaying @formstring from controller: " + @formstring
  @formstring = spaceinput
end

View 视图

<!--HTML page view -->
<p><%= @formstring %></p>

Rails Terminal Rails终端

Started POST "/pages/stringsub" for 127.0.0.1 at 2014-12-04 19:34:26 -0500
Processing by PagesController#stringsub as HTML
Parameters: {"mystring"=>"this should have spaces"}
Displaying @formstring from controller: this should have spaces
Rendered pages/stringsub.html.erb within layouts/application (1.4ms)
Completed 200 OK in 6ms (Views: 4.7ms | ActiveRecord: 0.0ms)

Rendered HTML 呈现的HTML

<!--HTML page view -->
<p>thisshouldhavespaces</p>

(note the spaces were removed in the final HTML) (请注意,空格已在最终的HTML中删除)

Is there a way to preserve the spaces in the original string that is sent? 有没有办法在发送的原始字符串中保留空格?

Edit: 编辑:

The issue was a method created by a group member of this project whose method had the original string passed to it as a parameter and it used .gsub to remove spaces from the parameter sent to it. 问题是由该项目的小组成员创建的方法,该方法的方法将原始字符串作为参数传递给它,并使用.gsub从发送给它的参数中删除空格。

Method that broke it 打破它的方法

def groupmethod(start)
    sub = start.split(" ")
    count = (start.length - start.gsub!(/\s+/, "").length + 1) * 0.4

Ironically I was actually able to fix it by creating a new variable out of it with String.new and passing that to the method instead 具有讽刺意味的是,我实际上可以通过使用String.new在其中创建一个新变量并将其传递给方法来修复它

Undamaged instance variable 完好的实例变量

forminput = params['mystring']
@formstring = forminput
testvariable = String.new(forminput)
groupmethod(testvariable)

Is there a better or more correct way of passing variables to ruby methods and use them separately without affecting the variable you passed to the method? 是否有更好或更正确的方法将变量传递给ruby方法并单独使用它们而不影响传递给该方法的变量?

Your stringsub method can be simplified. 您的stringsub方法可以简化。 Let's say you have a new action in your UsersController (simplified version): 假设您在UsersController (简化版)中有一个new操作:

def new
  set_str
end

private

def set_str
  @str = params['str']
  puts "@str: #{@str}"
end

The instance variable @str should be available to your view. 实例变量@str应该对您的视图可用。 Make sure the instance variable giving you trouble isn't being modified elsewhere in your application. 确保给您带来麻烦的实例变量不会在应用程序的其他地方被修改。 Single spaces will be preserved, but if you want to preserve all whitespace you could style your element with .your-class { white-space: pre; } 将保留单个空格,但是如果要保留所有空格,则可以使用.your-class { white-space: pre; } .your-class { white-space: pre; }

As an aside (in your original method), I don't see how the puts line outputs anything for @forminput when (apparently) it gets initialized at the very end. @forminput (在您的原始方法中),当(显然)它在最后初始化时, puts行不会为@forminput输出任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM