我正在尝试link_to这样:
<% @folder.contents.each do |content| %>
<%= link_to content_path do %>
<%= image_tag content.mfile.url(:thumbnail) %>
<% end %>
...
此时内容如下:
[6] pry(#<#<Class:0x007f30f8846568>>)> content
=> #<Content:0x007f30fb9add40
id: 41,
name: "jjjjjj",
url: nil,
created_at: Tue, 18 Apr 2017 13:36:51 UTC +00:00,
updated_at: Tue, 18 Apr 2017 13:36:51 UTC +00:00,
mfile_file_name: "Selection_016.png",
mfile_content_type: "image/png",
mfile_file_size: 71534,
mfile_updated_at: Tue, 18 Apr 2017 13:36:51 UTC +00:00,
folder_id: 5>
问题是content_path被赋予:
[7] pry(#<#<Class:0x007f30f8846568>>)> content_path
=> "/contents/5"
当我想要41时。
有任何解释或帮助解决这个问题吗?
答案 0 :(得分:0)
你试过了吗?
<%= link_to content_path(content) do %>
执行此操作时:
<%= link_to content_path do %>
Rails尝试猜测用于生成路径的适当id
。我猜测5
是id
的{{1}},并且您的参数包括@folder
。所以,如果我有这个权利,那么Rails在params中查找id,找到{..., id: 5, ...}
,并将其用作5
的猜测。
当你这样做时:
content_path
你不要让Rails猜测,你告诉Rails <%= link_to content_path(content) do %>
使用什么对象。
答案 1 :(得分:0)
这是一个常见的错误。如果你没有给任何rails路径路径提供id,它会从url或params中猜出它的id。
如果您使用的路径根指向另一个ID您必须指定必须用于创建路径的ID,您可以在此处指定为
C:\Users\Jon\Desktop>node stockinfo.js --compiled the jsfile
C:\Users\Jon\node_modules\selenium-webdriver\lib\promise.js:2634 --ERROR starts here
throw error;
^
Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:63925
at ClientRequest.<anonymous> (C:\Users\Jon\node_modules\selenium-webdriver\http\index.js:238:15)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:309:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1281:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
From: Task: WebDriver.navigate().to(https://www.google.com)
at thenableWebDriverProxy.schedule (C:\Users\Jon\node_modules\selenium-webdriver\lib\webdriver.js:816:17)
at Navigation.to (C:\Users\Jon\node_modules\selenium-webdriver\lib\webdriver.js:1140:25)
at thenableWebDriverProxy.get (C:\Users\Jon\node_modules\selenium-webdriver\lib\webdriver.js:997:28)
at Object.<anonymous> (C:\Users\Jon\Desktop\stockinfo.js:5:8)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
您在此处指定的ID 5必须是来自url或params的id。尝试在你的循环之前检查参数,你会得到它。
答案 2 :(得分:0)
content_path
是您在路径文件中放置的配置生成的方法。您可以按照建议传递参数,也可以使用此
<%= link_to content do %>
rails会知道用内容实例作为参数来调用content_path
方法。