从rails中的按钮调用控制器方法(Calling a controller method from a button in rails)
我之前已经多次询问过这个问题(比如这里 , 这里 ,仅举几例)但我无法理解我的情况。 我正在使用rails 5。
我在
posts_controller
有一个名为scrape
的方法。 它创建了几个对象实例。class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def scrape destroy_old_data @some_var = Scrape.create("something") end ... end
我想在
views/posts/index.html.erb
为scrape
方法创建一个按钮,但是我无法使按钮起作用。 我习惯使用脚手架方法,我不知道如何创建自定义方法。我试过的一些方法:
<%= button_to "scrape", action: "scrape"%> <%= "scrape me", { :controller => "posts", :action => "scrape"}, class: 'btn btn-primary' %> <%= button_to "scrape", posts_scrape_path %>
如何在
index.html.erb
中创建一个触发scrape
方法的scrape
按钮? 我是否还需要修改我的路线?I see this question has been ask several times before (i.e. here, here, just to name a few) but I can't get it working with my case. I am using rails 5.
I have in my
posts_controller
a method calledscrape
. It creates several object instances.class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def scrape destroy_old_data @some_var = Scrape.create("something") end ... end
I want to create a button in
views/posts/index.html.erb
forscrape
method, but I can't get the button to work. I am so used to use scaffold method that I don't know how to create a custom method.Some of the methods that I tried:
<%= button_to "scrape", action: "scrape"%> <%= "scrape me", { :controller => "posts", :action => "scrape"}, class: 'btn btn-primary' %> <%= button_to "scrape", posts_scrape_path %>
How can I create a
scrape
button inindex.html.erb
that triggersscrape
method? Do I need to modify my routes as well?
原文:https://stackoverflow.com/questions/41383168
最满意答案
请参阅POI公式评估文档 。 完成对电子表格的更改后,您需要重新计算公式评估,因为文件格式会缓存最后一个值以快速加载。
See the POI Formula Evaluation Documentation. Once you are done making changes to the spreadsheet, you'll need to recalculate the formula evaluations, as the file format caches the last value to make loading quick.
相关问答
更多-
你可以使用sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo); 示例sheet.addMergedRegion(new CellRangeAddress(1,1,4,1)); 将从B2合并到E2。 记住它是基于零的索引。 有关详细信息,请参阅BusyDeveloper指南 You can use sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo); example sheet.addMergedRegion( ...
-
使用Apache POI及其名称获取Excel公式的内容(Get Content of a Excel Formula with Apache POI and its name)[2022-10-21]
我相信您要查找的术语是命名范围 (尽管它们在某些版本的Excel中的功能区的“公式”选项卡中定义)。 假设你是这个意思: 然后,Apache POI需要的关键方法是: Workbook.getNumberOfNames() Workbook.getName(字符串) Workbook.getNameAt(INT) Workbook.createName() 如果你知道范围的名称,并想要得到它所指的单元格,你就会做类似的事情 Workbook wb = WorkbookFactory.create(new F ... -
可以按CTRL-ALT-F9手动在Excel中强制重新评估所有公式。 这里有个诀窍让它在工作簿打开时自动运行。 将以下内容添加到您的公式中: +(NOW()*0) 例如,我的SUMPRODUCT在上面变成了 =SUMPRODUCT((DS!B:B="IN_THIS_ONLY")*(DS!D:D="New trade"))+(NOW()*0) 这工作! Excel现在重新计算我公开的特殊公式单元格。 原因是NOW()是一个易失性函数。 这里是我学习这个的地方: http : //msdn.microsof ...
-
您需要“评估”单元格以获得公式的结果。 这不是由POI自动完成的,因为它可能是一项繁重的操作,往往不是必需的。 有关详细信息,请参阅http://poi.apache.org/spreadsheet/eval.html ,基本上,您将创建一个FormulaEvaluator并为有问题的单元检索CellValue FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); ... CellValue cellVal ...
-
请参阅POI公式评估文档 。 完成对电子表格的更改后,您需要重新计算公式评估,因为文件格式会缓存最后一个值以快速加载。 See the POI Formula Evaluation Documentation. Once you are done making changes to the spreadsheet, you'll need to recalculate the formula evaluations, as the file format caches the last value to m ...
-
使用Apache POI Excel写入特定的单元格位置(Writing to a particular cell location using Apache POI Excel)[2022-09-27]
当然,这很容易,只要记住POI是0而不是1就是基于寻址。 假设你想写第十行,第四列,你会做类似的事情 Row r = sheet.getRow(9); // 10-1 if (r == null) { // First cell in the row, create r = sheet.createRow(9); } Cell c = r.getCell(3); // 4-1 if (c == null) { // New cell c = r.createCell(3, Ce ... -
对于任何公式单元格,使用poi3.8。 Workbook xlsWorkbook = null; Cell cell = null; FormulaEvaluator formulaEval = xlsWorkbook.getCreationHelper().createFormulaEvaluator(); String value=formulaEval.evaluate(cell).formatAsString(); System.out.println("Formula Cell value : ...
-
使用Apache POI在Excel中的一定数量的单元格后字体消失(Font disappears after a certain amount of cells in Excel using Apache POI)[2023-09-02]
Apache POI限制为每个工作簿32767个字体 您需要找到一种重用字体和单元格样式的方法。 Apache POI is limited to 32767 Fonts per workbook You will need to find a way to reuse the fonts and cell styles. -
如果已将特定样式应用于整个列,则可以使用Sheet的getColumnStyle方法检索该CellStyle ,并将基于0的列索引传递给它。 它检索一个普通的CellStyle对象,可以在其他任何地方使用CellStyle ,例如在Cell的setCellStyle方法中 。 为了避免NullPointerException ,如果行或单元格不存在,则getRow和getCell可以返回null 。 您需要调用createRow和/或createCell来创建Cell ,您可以随时调用setCellStyl ...
-
尝试为工作簿创建单元格样式,并为列中的所有单元格应用"#,##0"数字格式: CellStyle style = workbook.createCellStyle(); DataFormat format = workbook.createDataFormat(); style.setDataFormat(format.getFormat("#,##0")); cell.setCellStyle(style); 从工作簿创建样式是最佳方式,不会使内存过载。 您还可以调整公式以将数字舍入为0位小数: =RO ...