当我遇到一些真实性令牌问题时,我正在研究一个新的Rails 4应用程序(在Ruby 2.0.0-p0上).
在编写响应json的控制器时(使用respond_to类方法),当我尝试使用创建记录时,我create开始ActionController::InvalidAuthenticityToken尝试异常curl.
我确定我设置-H "Content-Type: application/json"并设置数据,-d "<my data here>"但仍然没有运气.
我尝试使用Rails 3.2(在Ruby 1.9.3上)编写相同的控制器,并且我没有任何真实性令牌问题.我四处搜索,看到Rails 4中的真实性令牌发生了一些变化.据我所知,它们不再自动插入表格了?我想这会以某种方式影响非HTML内容类型.
有没有办法解决这个问题,而无需请求HTML表单,抢夺真实性令牌,然后使用该令牌发出另一个请求?还是我完全错过了一些非常明显的东西?
编辑:我刚尝试使用脚手架在新的Rails 4应用程序中创建一个新记录而不改变任何东西,我遇到了同样的问题所以我想这不是我做的事情.
启动一个新的应用程序,当我创建一个控制器页面回家并尝试转到本地主机:3000/pages/home时,我收到以下错误:
Showing c:/Users/Doesha/desktop/pinplug/app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method
(in c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/turbolinks-2.5.3/lib/assets/javascripts/turbolinks.js.coffee)
Run Code Online (Sandbox Code Playgroud)
application.html.erb文件:
<!DOCTYPE html>
<html>
<head>
<title>Pinplug</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
application_controller.rb文件:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
Run Code Online (Sandbox Code Playgroud)
pages_controller.rb文件:
class PagesController < …Run Code Online (Sandbox Code Playgroud) 我的应用程序在开发环境中运行时工作正常.在production(rails server -e production)中,浏览器无法访问css和js文件,并且在控制台上有以下消息:
I, [2013-07-27T21:00:59.105459 #11449] INFO -- : Started GET "/javascripts/application.js" for 99.102.22.124 at 2013-07-27 21:00:59 +0000
F, [2013-07-27T21:00:59.108302 #11449] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
Run Code Online (Sandbox Code Playgroud)
在生产环境中来自html源头部分:
<head>
<title>a Social Server</title>
<link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<script data-turbolinks-track="true" src="/javascripts/application.js"></script>
<meta content="authenticity_token" name="csrf-param">
<meta content="jYM4IAXTXAuKWeD4FEVrXgXRNFeB6EazU68ZBQfRqNY=" name="csrf-token">
</head>
Run Code Online (Sandbox Code Playgroud)
另一方面,在开发环境中,头部看起来像:
<head>
<title>a Social Server</title>
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet">
<link data-turbolinks-track="true" href="/assets/twitter-bootstrap-static/bootstrap.css?body=1" media="all" rel="stylesheet">
<link data-turbolinks-track="true" href="/assets/twitter-bootstrap-static/fontawesome.css?body=1" media="all" rel="stylesheet">
<link data-turbolinks-track="true" href="/assets/bootstrap_and_overrides.css?body=1" media="all" …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 3,并且有一个页面可以输出数据库中的帖子列表.我希望能够从链接中删除帖子.
下面的第二个例子有效,但第一个没有.谁知道为什么第一个不起作用?我的观点包含:
# this link sends a "GET" request which asks for the #show function
<%= link_to 'Delete', post, :method => :delete %>
# this link sends the proper "DELETE" request which asks for the #destroy function
<%= button_to 'Delete', post, :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
我的路线文件包含以下内容:
resources :posts
Run Code Online (Sandbox Code Playgroud) 这是我在rails中的第一个项目,即创建一个存储游戏数据的表.我能够从表格中显示关于赢家得分,输家得分等的数据.但是,我的表格列中存在包含每个游戏的删除链接的问题.
这是我在删除方法的游戏控制器中的代码:
def delete
@game = Game.find(params[:game])
@game.destroy()
redirect_to :action => 'index'
end
Run Code Online (Sandbox Code Playgroud)
我的表代码片段,其中包含link_to命令的行
<% @games_items.each do |t| %>
<tr>
<td><%= t.winner.name %></td>
<td><%= t.loser.name %></td>
<td><%= t.challenger.name %></td>
<td><%= t.winner_score %></td>
<td><%= t.loser_score %></td>
<td><%= link_to 'Delete', delete_game_path(id: t.id)%></td>
</tr>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在我调用的路由文件中
resources :games
Run Code Online (Sandbox Code Playgroud)
据我所知,这有助于生成基本路由.任何人都可以帮我弄清楚为什么我的link_to不起作用?
所以我的问题,从我可以看出,似乎是ruby没有加载它的所有javascript文件,因为我没有得到确认消息和link_to与方法:: delete对我没有任何作用.
随着默认设置提供给我rails新
views/layouts/application.html.erb head section如下.
<head>
<title>Myrubyblog</title>
<%= stylesheet_link_tag 'default', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true%>
<%= csrf_meta_tags %>
</head>
Run Code Online (Sandbox Code Playgroud)
这给了我错误
Started GET "/posts" for 127.0.0.1 at 2015-02-27 02:14:57 -0600
ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PostsController#index as HTML
Post Load (1.0ms) SELECT "posts".* FROM "posts"
Rendered posts/index.html.erb within layouts/application (11.0ms)
Completed 200 OK in 76ms (Views: 54.0ms | ActiveRecord: 4.0ms)
Started GET "/stylesheets/default.css" for 127.0.0.1 at 2015-02-27 02:14:58 -0600 …Run Code Online (Sandbox Code Playgroud) 我是 Ruby On Rails 的新手,正在阅读本指南以构建基本应用程序。
当我尝试实现文档中提到的删除功能时,我看到了显示页面。
我的控制器中有以下方法:
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
Run Code Online (Sandbox Code Playgroud)
在我的页面下面一行:
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
Run Code Online (Sandbox Code Playgroud)
现在,当我点击链接时,我在浏览器中看到以下 URL:
现在,在这种情况下,我看到的是显示屏幕,而不是收到警报消息,然后从我的页面中删除记录。
我已经关注了这篇 SO post - Rails 4 link_to Destroy not working in Getting Started tutorial
并证实
//= require jquery
//= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)
元素在我的 application.js
请告诉我我在哪里做错了?
更新:
这是我的rake routes命令输出:
E:\Rails\blog\bin>rake routes
(in E:/Rails/blog)
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
articles …Run Code Online (Sandbox Code Playgroud) 我跟随迈克尔·哈特尔(Michael Hartl)第13章的导轨教程,但不是微博,我正在创造动物.
我对动物的看法显示了一个"删除"超链接,该超链接应该从我的列表中删除动物记录,但是视图动作似乎没有达到它使用destroy方法的程度.
我在这里阅读了类似帖子中的所有答案,但在我的案例中找不到那些答案.
我正按照本教程中的说明,在AWS cloud9上的开发环境中.我真的很感激任何指针,因为我几天都在努力争取这个.
这是我的视图中的代码:
<li id="animal-<%= animal.id %>">
<%= link_to gravatar_for(animal.user, size: 50), animal.user %>
<span class="user"><%= link_to animal.user.name, animal.user %></span>
<span class="ear_tag"><%= animal.ear_tag %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(animal.created_at) %> ago.
<% if current_user?(animal.user) %>
<%= link_to "delete", animal, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
Run Code Online (Sandbox Code Playgroud)
从Feed视图调用此视图:
<% if @feed_items.any? %>
<ol class="animals">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %> …Run Code Online (Sandbox Code Playgroud)