看来现在在Rails 4中使用资产管道和sprocket-rails gem,当处理图像时,它们的文件名附加了像css和javascript这样的md5指纹.虽然这很有意义,因为md5指纹非常棒,但是从javascript访问该图像变得越来越困难.在rails 3.2中,我可以访问图像/assets/image_name.jpg并且它可以正常使用,但是在轨道4中资产不存在,它只存在名称中的md5指纹.
我知道rails提供帮助来通过erb访问图像,<%= asset-url("image_name.jpg") %>但这在javascript中不太理想,因为我没有在我的js中使用erb.有很多方法可以通过在视图中使用数据属性或在我的视图中使用脚本标记并设置一些全局变量来解决这个问题,但我正在寻找一个很好的解决方案,如果它存在的话.
任何帮助表示赞赏,谢谢.
我目前正在尝试为模型创建一个表单,该模型具有动态数量的嵌套模型.我正在使用嵌套表单(如RailsCasts 197中所述).为了使事情变得更复杂,我的每个嵌套模型都has_one与第三个模型相关联,我也希望将其添加到表单中.
对于任何想知道过度规范化或不正确方法的人来说,这个例子是我面临的问题的简化版本.实际上,事情稍微复杂一点,这就是我们决定采取的方法.
一些示例代码来说明下面的问题:
#MODELS
class Test
attr_accessible :test_name, :test_description, :questions_attributes
has_many :questions
accepts_nested_attributes_for :questions
end
class Question
attr_accessible :question, :answer_attributes
belongs_to :test
has_one :answer
accepts_nested_attributes_for :answer
end
class Answer
attr_accessible :answer
belongs_to :question
end
#CONTROLLER
class TestsController < ApplicationController
#GET /tests/new
def new
@test = Test.new
@questions = @test.questions.build
@answers = @questions.build_answer
end
end
#VIEW
<%= form_for @test do |f| %>
<%= f.label :test_name %>
<%= f.text_box :test_name %>
<%= f.label :test_description %>
<%= f.text_area …Run Code Online (Sandbox Code Playgroud) 我有一个网格状的无序列表,可以旋转144(90px x 90px)图像(12x12).我的最终目标是采用144图像网格并将其保存为1张图像.
我目前的解决方案让我遵循以下步骤:
当我遍历图像时,我会跟踪指针(我当前在画布上的位置).我这样做是通过维护行和列号来实现的.它们代表我正在绘制的图像的当前行和列.我使用它们,乘以单个图像的宽度和高度,以获得画布上的精确x和y坐标以绘制下一个图像.
当我调用函数来创建,绘制和生成画布的base64时,我收到以下错误消息:"InvalidStateError:尝试使用不可用或不再可用的对象.".如果这个错误在100%的时间内发生,我会假设它,因为我正在绘制到画布的图像,要么尚未加载,要么根本没有加载,但是,我只收到一次此错误我加载的每个新图像.例如,如果我有一个144图像网格,即每次绘制72次的2个不同图像,我将收到两次InvalidStateError,然后第三次调用该函数,它将成功.
请记住,这只是用于测试保存图像的尖峰代码,我知道需要进行一些重构.
generateThumbnail: function(){
var builder = this,
canvas = document.createElement('canvas'),
content,
row = 0,
col = 0;
// width is single image width (90) x number of tiles wide (usually 12)
canvas.width = 90 * builder.grid[0];
// height is single image height (90) x number of tiles high (usually 12)
canvas.height = 90 * builder.grid[1];
// get 2d context of new canvas
context …Run Code Online (Sandbox Code Playgroud) 首先加载全局应用程序控制器时,在该名称空间中加载页面时,不会加载命名空间的应用程序控制器.应用程序控制器如下所示:
class ApplicationController < ActionController::Base
protect_from_forgery
end
Run Code Online (Sandbox Code Playgroud)
命名空间的应用程序控制器如下所示:
class Admin::ApplicationController < ApplicationController
def authenticate_admin!
if current_admin.nil?
redirect_to new_admin_session_url
end
end
private
def current_admin
@current_admin ||= Admin.find(session[:admin_id]) if session[:admin_id]
end
helper_method :current_admin
end
Run Code Online (Sandbox Code Playgroud)
当我们使用before_filter"authenticate_admin!"时 像这样:
class Admin::AssetsController < Admin::ApplicationController
before_filter :authenticate_admin!
end
Run Code Online (Sandbox Code Playgroud)
抛出"Admin :: AssetsController #new中的NoMethodError".仅当我们在命名空间路由之前命中全局路由时才会发生这种情况.如果服务器重新启动并且加载了命名空间路由,则一切正常.
ruby ruby-on-rails controllers nomethoderror ruby-on-rails-3.2
重新启动我的虚拟机后,每当我尝试访问任何数据时,我都会收到此错误.我正在使用Rails 3.2.3和Mongodb w/Mongoid.这是我使用MongoDB和Mongoid的第一个项目,我相当肯定这个问题与它有关.似乎Mongo已经开始运行了.完整的错误消息是:
NoMethodError: undefined method `-' for nil:NilClass
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/cluster.rb:118:in `block in refresh'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/cluster.rb:125:in `each'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/cluster.rb:125:in `refresh'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/cluster.rb:78:in `nodes'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/cluster.rb:187:in `with_secondary'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/session/context.rb:104:in `with_node'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/session/context.rb:43:in `query'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/moped-1.2.5/lib/moped/query.rb:109:in `first'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/mongoid-3.0.6/lib/mongoid/contextual/mongo.rb:201:in `first'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/mongoid-3.0.6/lib/mongoid/contextual.rb:18:in `first'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/mongoid-3.0.6/lib/mongoid/finders.rb:119:in `first'
from (irb):3
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/development/.rvm/gems/ruby-1.9.3-p0@applicationName/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
Run Code Online (Sandbox Code Playgroud) ruby ×2
canvas ×1
controllers ×1
html5 ×1
javascript ×1
mongodb ×1
mongoid ×1
nested-forms ×1
ruby-1.9.3 ×1
sprockets ×1