我有一个扫地机应该到期一些动作缓存.即使调试器在调用expire_action之前立即停止,它实际上并没有使操作到期.知道会发生什么吗?
这是相关的清扫车和控制器.
#company_sweeper.rb(在'models'目录中)
class CompanySweeper < ActionController::Caching::Sweeper
observe Company
def after_save(company)
expire_cache(company) if company.final_save && company.valid?
end
def expire_cache(company)
debugger <= #debugger stops here!
right before the call
I'm trying to make.
expire_action :controller => 'reports',
:action => 'full_report'
end
end
Run Code Online (Sandbox Code Playgroud)
#reports_controller.rb
class ReportsController < ApplicationController
layout false
caches_action :full_report, :supplier_list, :service_categories
cache_sweeper :company_sweeper
def full_report
#do stuff...
end
end
Run Code Online (Sandbox Code Playgroud)
我知道它没有到期的方式是完整的报告返回旧数据,并几乎立即响应.很奇怪,对吗?
我需要在我的rails应用程序中的某个环境中强制主机.
我可以通过包括使覆盖工作
def default_url_options(opts={})
opts.merge({:host => 'stg.my-host.com'})
end
Run Code Online (Sandbox Code Playgroud)
在app/controllers/application.rb中
但有没有办法在初始化时设置它,最好是在config/environments/...文件中?我想将条件env逻辑保留在控制器之外.
但是,当我尝试
config.action_controller.default_url_options = { ... }
Run Code Online (Sandbox Code Playgroud)
甚至
ActionController::Base.default_url_options = { ... }
Run Code Online (Sandbox Code Playgroud)
我得到"未定义的方法",即使在config.after_initialize {...}中包装
有什么想法吗?
我没有使用jQuery验证检查当前密码.我正在使用Devise gem和jQuery验证.萤火虫说:
问题出在我的控制器中:
def checkpass
user = User.find_by_email(params[:user][:email])
respond_to do |format|
if user && user.authenticate(params[:user][:password])
format.json { render :json => @user }
end
Run Code Online (Sandbox Code Playgroud)
结束
使用此代码,Firebug给了我错误
406 Not Acceptable
end
Run Code Online (Sandbox Code Playgroud)
checkemail唯一性动作的例子:
def checkemail
@user = User.find_by_email(params[:user][:email])
respond_to do |format|
format.json { render :json => !@user }
end
end
Run Code Online (Sandbox Code Playgroud)
我的js:
$("#edit_password").validate({
"user[current_password]":{
required: true,
minlength: 6,
remote: {
url: "http://127.0.0.1:3000/checkpass"
}
...
})
Run Code Online (Sandbox Code Playgroud)
和HTML:
<input type="password" size="30" name="user[current_password]" id="user_current_password" class="valid">
Run Code Online (Sandbox Code Playgroud)
有人可以建议怎么做吗?
在iOS 7中,我通过"showFromRect"显示actionSheet:
[actionSheet showFromRect:rect inView:view animated:YES];
Run Code Online (Sandbox Code Playgroud)
但在iOS 8中,这不起作用.他们替换实现并建议我们使用UIAlertController.那么如何像弹出框一样显示这个actionSheet?
关于嵌套参数有很多问题,但我似乎无法找到解决我特定的简单情况的问题.
我正在尝试允许不是数组的嵌套哈希.我希望这可行:
params.require(:book).permit(:title, :description, style: {:font, :color})
Run Code Online (Sandbox Code Playgroud)
但它导致语法错误.
然而,这有效:
params.require(:book).permit(:title, :description, style: [:font, :color])
Run Code Online (Sandbox Code Playgroud)
但我的问题是,它似乎允许style
值为具有属性的项目数组:font和:color.我只想允许具有这两个属性的单个哈希.
我尝试了其他变体,但我不断收到语法错误.我很感激任何帮助.
上下文:Rails 4.1.7,Ruby 2.0.0(它在我的todo列表中进行升级!),而不是使用ActiveRecord.
我刚刚在Rails 5.0.0beta3上创建了一个带有几个表单的简单应用程序.
在开发中,使用Safari或Chrome上的http:// localhost:3000来访问应用程序,如果我填写表单并提交它,我总是会收到ActionController::InvalidAuthenticityToken
错误.但是,如果我在填写并提交之前重新加载页面,那么它可以正常工作.
该应用使用默认值:
protect_from_forgery with: :exception
在ApplicationController中,<%= csrf_meta_tags %>
在html标题中通过应用程序布局,例:
<%= form_for @node, url: admin_book_nodes_url, as: :node do |form| %>
<%= render "form", f: form %>
<p><%= form.submit %> or <%= link_to "Cancel", admin_book_nodes_path %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
日志:
Started POST "/admin/book/nodes" for ::1 at 2016-03-20 11:54:31 +0000
Processing by Admin::Book::NodesController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"/G5pF6hSPx0Vf21Fi0FCh+VlOcHY4w8C5lmHmwr3NQRjfXUP9/xboybeV3tevmyTyHcwSX8LplU/HgZVGDbGlw==", "node"=>{"parent_id"=>"1", "position"=>"1", "title"=>"lkjlkj", "description"=>"lkjlj", "published"=>"0", "content"=>"lkjlkj"}, "commit"=>"Create node"}
Can't verify CSRF token authenticity
Completed 422 …
Run Code Online (Sandbox Code Playgroud) ruby-on-rails actioncontroller csrf-protection ruby-on-rails-5
所以......第一次向StackOverflow提问......
我按照 Rails 指南和 RailsApps 项目将现有的 Rails 4.2.5 应用程序(使用 Ruby 2.2.4)转换为 Rails 5.1.3 应用程序(使用 Ruby 2.4.1)。
启动开发服务器会产生:
Taruns-iMac:Play_Ball tarunchattoraj$ rails s
=> Booting Puma
=> Rails 5.1.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Run Code Online (Sandbox Code Playgroud)
这是我所期望的。
但是,导航到 localhost:3000 会产生 ActionController 错误:
Started …
Run Code Online (Sandbox Code Playgroud) 我有许多控制器会有一些类似的行为,例如用户应该登录,需要设置一些范围,需要设置current_account/current_user并缓存权限.
我想到使用标准控制器并对其进行子类化.
class MyStandardController < ApplicationController
before_filter :xyz
end
class SomeController < MyStandardController
end
Run Code Online (Sandbox Code Playgroud)
我想知道的是我需要/什么时候打电话super
?
谢谢
我只有 2 周的时间学习 ruby on rails,在我的应用程序中,用户可以注册他们的汽车,从他们的个人资料(下面的代码)应用程序发送到注册汽车页面,
<div class="container">
<fieldset>
<h1><%= @user.email %></h1>
<br>
<h2>now you are able to...</h2>
<br>
<ul>
<li>
<strong>new car registration: </strong>
<%= link_to "new car", new_user_car_path(current_user)%>
</li>
</ul>
</fieldset>
</div>
Run Code Online (Sandbox Code Playgroud)
它以前有效,但我不知道我做了什么,现在它显示了:
Routing Error
No route matches {:action=>"show", :user_id=>#<User id: 27, email: "armando.santoya@hotmail.com", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"}
Try running rake routes for …
Run Code Online (Sandbox Code Playgroud) 使用Rails 4播放的时候,我注意到,它定义了一个before_action
过滤器set_[model]
,即要求采取的行动show
,edit
,update
和destroy
.
事实上它非常有用,但我认为没有对所有成员行动都没有多大意义.毕竟,如果您正在使用成员操作,您希望对该成员执行操作,因此您需要在某个时刻从数据库中恢复它.
请注意,通过成员操作,我还意味着routes.rb
在members
块中配置的那些操作.
是否有直接的方法来做到这一点,没有列出before_action
过滤器上的所有成员操作?
编辑:为了澄清,整点是使用一些rails魔法来获取所有成员路由并生成将在其中传递的数组:only
.所以我可以做点什么
before_action set_model only: all_member_routes
Run Code Online (Sandbox Code Playgroud)
其中all_member_routes
一段代码返回我的模型的所有成员路由.
甚至更好,
before_action set_model, only_member_actions: true
Run Code Online (Sandbox Code Playgroud)