如何在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">×</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">×</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
最满意答案
尝试这个:
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')
-
Android - 解码没有StringEscapeUtils的unicode字符?(Android - decode unicode characters without StringEscapeUtils?)[2022-03-17]
仅仅是我还是仅仅使用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.
-
在url中有unicode字符(have unicode characters in url)[2021-10-03]
你需要在字符串前面的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 ...