我有兴趣获取params哈希的嵌套'name'参数.打电话给像
params[:subject][:name]
Run Code Online (Sandbox Code Playgroud)
当params [:subject]为空时抛出错误.为了避免这个错误,我通常写这样的东西:
if params[:subject] && params[:subject][:name]
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的方法来实现这一点?
我有一个Rails应用程序,它使用我正在积极开发的宝石.如何指示应用程序在每次请求时重新加载gem?
我们都看到了辉煌的复杂形式railscast,其中Ryan Bates解释了如何使用Javascript在父对象表单中动态添加或删除嵌套对象.
有没有人对如何修改这些方法以便与Haml Formtastic合作有任何想法?
在这里添加一些上下文是我目前面临的问题的简化版本:
#教师表格(有嵌套的主题表格)[来自我的申请]
- semantic_form_for(@teacher) do |form|
- form.inputs do
= form.input :first_name
= form.input :surname
= form.input :city
= render 'subject_fields', :form => form
= link_to_add_fields "Add Subject", form, :subjects
Run Code Online (Sandbox Code Playgroud)
#个人主题形式部分[来自我的申请]
- form.fields_for :subjects do |ff|
#subject_field
= ff.input :name
= ff.input :exam
= ff.input :level
= ff.hidden_field :_destroy
= link_to_remove_fields "Remove Subject", ff
Run Code Online (Sandbox Code Playgroud)
#Application Helper(直接来自Railscasts)
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, …Run Code Online (Sandbox Code Playgroud) 我在一个宝石中遇到了这个结构并且不明白其意图.我知道单独使用tap,但为什么有人会调用super.tap {}而不是仅仅在超级调用之后继续执行代码.
def my_method
super.tap do |u|
if @user && @user.new_record?
@omniauth = u.session[:session]
u.session[:omniauth] = nil unless @user.new_record?
end
end
end
Run Code Online (Sandbox Code Playgroud)
任何人都可以开导我吗?
我经常需要克隆生产数据来调查错误.即使有一个简单的数据库大小heroku db:拉(点击)需要5分钟以上,似乎很有可能失败.是否有另一种方法来提取数据库?
还将赞赏替代流程/文章的图书馆.
我经常想对数组执行X次操作,然后返回除该数字之外的结果.我经常写的代码如下:
def other_participants
output =[]
NUMBER_COMPARED.times do
output << Participant.new(all_friends.shuffle.pop, self)
end
output
end
Run Code Online (Sandbox Code Playgroud)
有更清洁的方法吗?
用于将"deleted_at"时间列转换为日期时间列的Rails迁移失败.关于如何解决这个问题的任何想法?如果相关的话,这是Postgres的全新安装.
-- change_column(:products, :deleted_at, :datetime)
PGError: ERROR: column "deleted_at" cannot be cast to type timestamp without time zone
: ALTER TABLE "products" ALTER COLUMN "deleted_at" TYPE timestamp
Run Code Online (Sandbox Code Playgroud) 我正在使用Spree,它广泛使用Rails引擎.我发现的常见模式是这个.但是我无法理解这段代码.
class Engine < Rails::Engine
def self.activate
...
end
config.to_prepare &method(:activate).to_proc
end
Run Code Online (Sandbox Code Playgroud)
.to_prepare什么&method运营商吗?非常感谢!
我经常发现自己使用mv重命名文件.例如
mv app/models/keywords_builder.rb app/models/keywords_generator.rb
Run Code Online (Sandbox Code Playgroud)
这样做我需要写第二个参数的路径(确定,标签完成).在这个例子中它不是太糟糕,但有时路径是深度嵌套的,它似乎是相当多的额外打字.
有没有更有效的方法来做到这一点?
我正在努力让coffeescript与Sinatra合作.我对这两种技术都不熟悉,所以这可能是愚蠢的.我的问题似乎是coffeescript编译为javascript但不在页面上执行,而是显示为html.
#sinatra app
require 'coffee-script'
get "/test.js" do
coffee :hello
end
#hello.coffee
alert "hello world"
#My page (/test.js) doesn't execute the js - just displays the code
#On screen in the browser I get this:
(function() {
alert("hello world");
}).call(this);
#In the HTML I get this within the body tags
<pre style="word-wrap: break-word; white-space: pre-wrap;">(function() {
alert('hello world!');
}).call(this);
</pre>
Run Code Online (Sandbox Code Playgroud) ruby ×3
bash ×1
code-cleanup ×1
coffeescript ×1
formtastic ×1
hashmap ×1
heroku ×1
javascript ×1
linux ×1
mv ×1
nested-forms ×1
null ×1
postgresql ×1
sinatra ×1
spree ×1