首页 \ 问答 \ 如何在rails上将本地字符串传递给ruby中的部分(How to pass a local string to a partial in ruby on rails)

如何在rails上将本地字符串传递给ruby中的部分(How to pass a local string to a partial in ruby on rails)

TL; DR版本的问题

是否有可能将字符串传递给partial,并在if语句或switch case中部分使用该字符串。

我是否通过让观点直接对话来打破Rails的一些有价值的规则?

长版本w /代码片段

我认为可以肯定地说,在这个时候,我读过的每一篇文章都讲述了如何传递一个局部变量。 完成此操作仍然存在问题。 我对铁轨非常陌生,例如1周的学习,所以我想要做的事情可能完全不正确。

无论如何,我已经设置好了,这样当用户点击导航栏上的链接时,引导模式会以正确的形式加载它。 我已经将每个独特的形式设置为自己的部分,以便我可以放在需要的地方,并且还将模态设置为部分。 我试图遵循这个DRY方法,并使其在模态部分有一个if / else或switch逻辑,它只根据链接传递的内容加载正确的表单。

模态部分:

<!-- Modal -->
<div class="modal fade" id="modalTemplate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal Template</h4>
      </div>
      <div class="modal-body">
        <% if %VARIABLE% == "%PASSED-INPUT%" %>
          <% @customer = Customer.new %>
          <%= render "customers/new" %>
        <% elsif %VARIABLE% == "%PASSED-INPUT%" %>
          --> Do something else
        <% end %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

NavBar部分:

<%= render 'layouts/modal_template' %>

<nav class="navbar navbar-default navbar-fixed-side">
    <h6 class="navbar-heading">Customer Management</h6>
  <div class="list-group">
    <%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
    <%= link_to "Manage Customers", '#', class: 'list-group-item' %>
    </div>
    <h6 class="navbar-section-heading">Customer Status Management</h6>
    <div class="list-group">
    <%= link_to "Add New Customer Status", new_customers_status_path, class: 'list-group-item' %>
    <%= link_to "Manage Customer Statuses", '#', class: 'list-group-item' %>
    </div>
</nav>

本页主视图:

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-lg-2">
      <%= render 'layouts/side-nav' %>
    </div>
    <div class="col-sm-9 col-lg-10">
      <div class="well">
        <%= render 'test' %>
        <%= render 'view_customers' %>
      </div>
    </div>
  </div>
</div>

现在,“增加新客户”link_to是给我带来最大麻烦的部分。 我尝试了在线提供的每种不同的组合,但它似乎总是基于使用对象或数据库中的某些东西的想法。 事情是,我只是试图加载一个基于所选按钮的不同页面,同时使用相同的模态部分。

link_to我试过的例子:

<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', :locals =&t; { what_to_render: "Test" }, class: 'list-group-item' %>

还有更多,但我已经删除了这么多的代码行,我已经失去了它们。 一些包括使用link_to与:partial和:locals


TL;DR Version of Question

Is it possible to pass a string to a partial and have the partial use that string in an if statement or switch case.

Am I breaking some valuable rule of Rails by having the views talk directly to each other?

Long Version w/ Code Snippets

I think it's safe to say that at this point in time I've read every post there is about how to pass a local variable. I'm still having trouble with accomplishing this. I'm very new to rails, like.. 1 week of learning, so what I'm trying to do might be completely incorrect.

Anyways, I have it set up so that when a user clicks a link on the navbar a bootstrap modal loads with the correct form inside of it. I've set each unique form as their own partial so that I can just drop there in where needed and I've also setup the modal as a partial. I'm trying to follow this DRY method and make it so that in the modal partial it has a if/else or switch logic that only loads the correct form based on what the link passes it.

Modal Partial:

<!-- Modal -->
<div class="modal fade" id="modalTemplate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal Template</h4>
      </div>
      <div class="modal-body">
        <% if %VARIABLE% == "%PASSED-INPUT%" %>
          <% @customer = Customer.new %>
          <%= render "customers/new" %>
        <% elsif %VARIABLE% == "%PASSED-INPUT%" %>
          --> Do something else
        <% end %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

NavBar Partial:

<%= render 'layouts/modal_template' %>

<nav class="navbar navbar-default navbar-fixed-side">
    <h6 class="navbar-heading">Customer Management</h6>
  <div class="list-group">
    <%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
    <%= link_to "Manage Customers", '#', class: 'list-group-item' %>
    </div>
    <h6 class="navbar-section-heading">Customer Status Management</h6>
    <div class="list-group">
    <%= link_to "Add New Customer Status", new_customers_status_path, class: 'list-group-item' %>
    <%= link_to "Manage Customer Statuses", '#', class: 'list-group-item' %>
    </div>
</nav>

The Main View for this Page:

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-lg-2">
      <%= render 'layouts/side-nav' %>
    </div>
    <div class="col-sm-9 col-lg-10">
      <div class="well">
        <%= render 'test' %>
        <%= render 'view_customers' %>
      </div>
    </div>
  </div>
</div>

Now, the "Added New Customer" link_to is the part that is giving me the most trouble. I've tried every different combination provided online, but it always seems to be based around the idea of working with an object or something from the database. Thing is, I'm just trying to load a different page based on the selected button all while using the same modal partial.

link_to Examples that I've Tried:

<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', what_to_render: "Test", class: 'list-group-item' %>
<%= link_to "Add New Customer", '#modalTemplate', 'data-toggle' => 'modal', :locals => { what_to_render: "Test" }, class: 'list-group-item' %>

There are more, but I've deleted so many lines of code at this point that I've lost them. Some included using the link_to with :partial and :locals


原文:https://stackoverflow.com/questions/41128098
更新时间:2021-03-12 10:03

最满意答案

尝试这个:

str.decode('utf-8',errors='ignore')

Try this:

str.decode('utf-8',errors='ignore')

相关问答

更多
  • 当你输入"我" ,Python解释器从终端获取本地字符集中该字符的表示,由于该字符集按字节逐字节存储该字符。 在我的UTF-8系统上,这是'\xe6\x88\x91' 。 在你的,因为你使用GB2312,它是'\xce\xd2' 。 这解释了变量a的价值。 当你输入u"我" ,Python解释器不知道我字符在哪个编码中。它的作用与普通字符串几乎相同:它将字符的字节存储在Unicode字符串中,解释每个字节作为一个Unicode码点,因此错误的结果u'\xce\xd2' (或者在我的盒子上, u'\xe6\x ...
  • 尝试这个: str.decode('utf-8',errors='ignore') Try this: str.decode('utf-8',errors='ignore')
  • 仅仅是我还是仅仅使用TextView的setText()方法更复杂? 无论如何,以下是使用给定的示例json(将示例放到资源并使用loadJSONFromAsset()读取它loadJSONFromAsset()在我的最终工作正常: JsonParser parser = new JsonParser(); JsonElement element = parser.parse(loadJSONFromAsset()); JsonObject obj = element.getAsJsonObject(); ...
  • 问题在于您使用Char8模块的包,它不适用于非拉丁文1数据。 相反,从文本中使用encodeUtf8 。 The problem is your usage of pack from the Char8 module, which doesn't work with non-Latin 1 data. Instead, use encodeUtf8 from text.
  • 你需要在字符串前面的r前面放一个u ,或者用unicode方法包装它: 有关详细信息,请参阅此 Unicode字符串很像字符串,但在语法中使用前面的'u'字符指定:u'abc',u“def”。 http://docs.python.org/library/stdtypes.html You need to put a u in front of r before the string, or wrap it in the unicode method: See this for more info. Uni ...
  • 您的数据文件由字节组成,这些字节的编码是除utf-8之外的某些编码。 您需要指定正确的编码。 open(output_filename, encoding=...) 我们没有完全可靠的方法告诉您它应该是什么编码。 但是由于 In [156]: print('\x93'.decode('cp1252')) “ (因为引号是一个很常见的字符)你可能想尝试使用 open(output_filename, encoding='cp1252') 在test_GenomeDiagram.py的第662行。 Yo ...
  • 在错误消息中,我看到它尝试在读取它时猜测文件中使用的编码,最后它使用编码cp1250来读取它(可能是因为Windows在系统中使用cp1250作为默认值),但它是不正确的编码,因此您将其保存为'utf-8' 。 所以你必须使用open( ..., encoding='utf-8') ,它不需要猜测编码。 # replacing '>' with '>' and '<' with '<' f = open('Table.html','r', encoding='utf-8') s = f.r ...
  • 您可以尝试使用ISO编码打开文件。 open('myfile.txt', encoding = "ISO-8859-1") you can try out to open file with ISO encoding. open('myfile.txt', encoding = "ISO-8859-1")
  • 您没有告诉Perl如何编码输出。 你需要添加 use open ':std', ':encoding(XXX)'; 其中XXX是终端期望的编码。 在unix盒子上,你通常需要 use open ':std', ':encoding(UTF-8)'; 在Windows机箱上,您通常需要 use Win32 qw( ); use open ':std', ':encoding(cp'.Win32::GetConsoleOutputCP().')'; You didn't tell Perl how to ...
  • 你有多确定你的文件是UTF8编码的? 对于您发布的小样本,UTF8解码失败, ü是“带有DIAERESIS的拉丁语小写字母”。 当编码为ISO-8859-1时, ü是'\xfc' 。 另外两种可能性是CSV文件是UTF-16编码的(UTF-16小端在Windows上是常见的),甚至是Windows-1252。 如果您的CSV文件是使用ISO-8859-X系列编码编码的; 任何ISO 8859-1 / 3/4/9/10/14/15/16编码ü为0xfc。 要解决,请使用正确的编码并打开文件,如下所示: fil ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)