I'm building a rails app based off of the railsguide
我正在构建一个基于railsguide的rails应用程序
http://guides.rubyonrails.org/getting_started.html
The syntax it calls for in the erb is...
它在erb中调用的语法是......
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
For some reason this reads as
由于某些原因,这读作为
https://localhost:3000/articles/[#]
where # is the given record to delete. In other words, I want to delete a record, and it interprets my code as showing said record.
其中#是要删除的给定记录。换句话说,我想删除一条记录,并将我的代码解释为显示所述记录。
What could I be doing wrong?
我能做错什么?
more info
更多信息
This is what is generated dynamically
这是动态生成的
<td>
<a rel="nofollow" data-method="delete" href="/articles/2">Destroy</a>
</td>
application.html.erb has the following...
application.html.erb具有以下内容......
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
controller definition
控制器定义
def destroy
@article= Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
Error associated with using 'application' in application.html.erb
在application.html.erb中使用'application'时出错
ExecJS::ProgramError in Welcome#index
Showing E:/scabase/app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method
(in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/turbolinks- 2.5.3/lib/assets/javascripts/turbolinks.js.coffee)
3 个解决方案
#1
1
This is completely normal. In the REST world a request uses a combination of a verb (GET, DELETE etc) and a URI that identifies the resource. If you are doing different things to the same resource (i.e. the same article) then only the verb changes. The show page for an object uses GET, the update action uses PATCH (PUT prior to rails 4.1) and destroying a record uses DELETE
这完全正常。在REST世界中,请求使用动词(GET,DELETE等)和标识资源的URI的组合。如果您对同一资源(即同一篇文章)执行不同的操作,则只会更改动词。对象的显示页面使用GET,更新操作使用PATCH(在rails 4.1之前的PUT)并且销毁记录使用DELETE
In practice links always result in GET requests and even forms only allow GET or POST, so rails emulates the remaining methods - clicking on a link with a data-method=destroy
attributes creates a form that posts to the URL with a hidden _method
input with value DELETE. This is done via some javascript that is including in newly generated rails apps.
在实践中,链接总是导致GET请求,甚至表单只允许GET或POST,因此rails模拟剩余的方法 - 单击带有data-method = destroy属性的链接会创建一个表单,该表单使用隐藏的_method输入发布到URL值DELETE。这是通过一些包含在新生成的rails应用程序中的javascript完成的。
You've got
你有
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
and you've indicated that you're getting an error loading default.js - I'd guess that you're following an old set of instructions because in a modern app (since rails 3.1) this should be
并且你已经表明你在加载default.js时遇到错误 - 我猜你正在遵循一套旧指令,因为在现代应用程序中(因为rails 3.1)这应该是
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Your application.js file should in addition contain (among other things)
您的application.js文件还应包含(除其他外)
//= require jquery
//= require jquery_ujs
(You can use prototype or other js libraries too)
(您也可以使用原型或其他js库)
#2
2
Most probably, Rails default javascripts are not loaded - specifically jquery_ujs. This javascript allows Rails to interpret DELETE calls, which are not totally browser compliant.
最有可能的是,Rails默认的javascripts没有被加载 - 特别是jquery_ujs。这个javascript允许Rails解释DELETE调用,这些调用并不完全符合浏览器标准。
The javascript you load is named 'default'. Rails standard is 'application'. Check if you have a 'default.js' inside assets/javascripts and what it contains. If you want Rails out of the box behaviour, just move to:
您加载的javascript名为“default”。 Rails标准是'应用程序'。检查assets / javascripts中是否有'default.js'及其包含的内容。如果您希望Rails开箱即用,只需转到:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
This should work as long as you did not change the application.js file.
只要您没有更改application.js文件,这应该可以工作。
#3
0
The code you show will do two things:
您展示的代码将执行两项操作:
1) it will create a javascript pop-up that asks "are you sure?". That has to run on the client, not on the server. So Rails generates a "javascript" link that looks like # in your browser.
1)它会创建一个javascript弹出窗口,询问“你确定吗?”。这必须在客户端上运行,而不是在服务器上运行。所以Rails在你的浏览器中生成一个看起来像#的“javascript”链接。
2) if you click yes, it will send a HTTP DELETE to your server. Unfortunately, browsers don't do this easily. So it generates more javascript code to do that (AJAX).
2)如果单击“是”,它将向您的服务器发送HTTP DELETE。不幸的是,浏览器不容易这样做。因此它会生成更多的javascript代码(AJAX)。
So what you are seeing not Ruby/Rails, but the Javascript code that Rails generated for you.
所以你看到的不是Ruby / Rails,而是Rails为你生成的Javascript代码。
#1
1
This is completely normal. In the REST world a request uses a combination of a verb (GET, DELETE etc) and a URI that identifies the resource. If you are doing different things to the same resource (i.e. the same article) then only the verb changes. The show page for an object uses GET, the update action uses PATCH (PUT prior to rails 4.1) and destroying a record uses DELETE
这完全正常。在REST世界中,请求使用动词(GET,DELETE等)和标识资源的URI的组合。如果您对同一资源(即同一篇文章)执行不同的操作,则只会更改动词。对象的显示页面使用GET,更新操作使用PATCH(在rails 4.1之前的PUT)并且销毁记录使用DELETE
In practice links always result in GET requests and even forms only allow GET or POST, so rails emulates the remaining methods - clicking on a link with a data-method=destroy
attributes creates a form that posts to the URL with a hidden _method
input with value DELETE. This is done via some javascript that is including in newly generated rails apps.
在实践中,链接总是导致GET请求,甚至表单只允许GET或POST,因此rails模拟剩余的方法 - 单击带有data-method = destroy属性的链接会创建一个表单,该表单使用隐藏的_method输入发布到URL值DELETE。这是通过一些包含在新生成的rails应用程序中的javascript完成的。
You've got
你有
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
and you've indicated that you're getting an error loading default.js - I'd guess that you're following an old set of instructions because in a modern app (since rails 3.1) this should be
并且你已经表明你在加载default.js时遇到错误 - 我猜你正在遵循一套旧指令,因为在现代应用程序中(因为rails 3.1)这应该是
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Your application.js file should in addition contain (among other things)
您的application.js文件还应包含(除其他外)
//= require jquery
//= require jquery_ujs
(You can use prototype or other js libraries too)
(您也可以使用原型或其他js库)
#2
2
Most probably, Rails default javascripts are not loaded - specifically jquery_ujs. This javascript allows Rails to interpret DELETE calls, which are not totally browser compliant.
最有可能的是,Rails默认的javascripts没有被加载 - 特别是jquery_ujs。这个javascript允许Rails解释DELETE调用,这些调用并不完全符合浏览器标准。
The javascript you load is named 'default'. Rails standard is 'application'. Check if you have a 'default.js' inside assets/javascripts and what it contains. If you want Rails out of the box behaviour, just move to:
您加载的javascript名为“default”。 Rails标准是'应用程序'。检查assets / javascripts中是否有'default.js'及其包含的内容。如果您希望Rails开箱即用,只需转到:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
This should work as long as you did not change the application.js file.
只要您没有更改application.js文件,这应该可以工作。
#3
0
The code you show will do two things:
您展示的代码将执行两项操作:
1) it will create a javascript pop-up that asks "are you sure?". That has to run on the client, not on the server. So Rails generates a "javascript" link that looks like # in your browser.
1)它会创建一个javascript弹出窗口,询问“你确定吗?”。这必须在客户端上运行,而不是在服务器上运行。所以Rails在你的浏览器中生成一个看起来像#的“javascript”链接。
2) if you click yes, it will send a HTTP DELETE to your server. Unfortunately, browsers don't do this easily. So it generates more javascript code to do that (AJAX).
2)如果单击“是”,它将向您的服务器发送HTTP DELETE。不幸的是,浏览器不容易这样做。因此它会生成更多的javascript代码(AJAX)。
So what you are seeing not Ruby/Rails, but the Javascript code that Rails generated for you.
所以你看到的不是Ruby / Rails,而是Rails为你生成的Javascript代码。