我有一个帖子模型.每个帖子都有一个标题和许多片段.
class Post < ActiveRecord::Base
has_many :snippets
end
class Snippet < ActiveRecord::Base
belongs_to :post
attr_accessible :position, :post_id
end
Run Code Online (Sandbox Code Playgroud)
我想要4 种不同类型的片段,即:
我应该创建四个新模型(称为文本,代码,视频和图像)来扩展代码片段模型吗?:
class Text < Snipppet
attr_accessible :body
end
class Image < Snippet
attr_accessible :image
end
class Video < Snippet
attr_accessible :title
end
class Code < Snippet
attr_accessible code
end
Run Code Online (Sandbox Code Playgroud)
当每个代码段可以是4种不同的内容之一时,如何在视图中引用每个代码段的内容?
在我看来,我想要这样的东西:
- for snippet in @post.snippets
= place the content of the snippet here
Run Code Online (Sandbox Code Playgroud)
我不认为在片段模型上有一个"类型"字段是个好主意,因为这可能会导致数据库和代码的强耦合.在这种情况下,是否有某种轨道魔法会帮助我?
任何使用过rails_admin gem的人都可能注意到导航侧栏中的标签总是多元化的.
反正是否有针对特定型号的阻止?
典型的rails控制器可能会执行以下操作:
class FoosController < ApplicationController
def index
@foos = Foo.all
end
end
Run Code Online (Sandbox Code Playgroud)
我对Rails的理解很充分,知道@foos它将返回Foo对象的数组,但是它@foos本身是一个实例变量。
那么实例变量属于哪个对象?它是FoosController类的实例吗?每次访问索引页面时,是否都创建了该对象的另一个实例?如果我访问显示页面,其中@foo引入了一个新变量,该怎么办:
def show
@foo = Foo.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
这个变量与@foos属于同一个对象吗?
我可能在这里混淆机架和水豚方法
let!(:admin){FactoryGirl.create(:admin)}
# test passes
describe "visiting #edit page" do
before { visit edit_user_path(admin) }
specify { current_path.should eq(edit_user_path(admin)) }
end
# test fails
describe "getting #edit page" do
before { get edit_user_path(admin) }
specify { current_path.should eq(edit_user_path(admin)) }
end
Run Code Online (Sandbox Code Playgroud)
第二次测试失败了:
Failure/Error: specify { current_path.should eq(edit_user_path(admin)) }
expected: "/users/51/edit"
got: "/users/51"
(compared using ==)
Run Code Online (Sandbox Code Playgroud)
一个before(:each)块,将current_path设置为/users/51,所以看起来它在使用时仍然保持这种状态get.
我只想在这里查看:
在我的rails 4 app中,我有一个三重嵌套路线:
devise_for :users do
resources :foo do
resources :marflar
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个用于创建带有嵌入式Marflar对象的新Foo的表单:
<%= form_for(@foo) do |f| %>
<%= f.text_field :foo_attr %>
<%= f.fields_for :marflars_attributes do |marflars_form| %>
<%= marflars_form.text_field :marflar_attr %>
<% end %>
<%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但是当我提交表格时,我得到:
TypeError in FoosController#create
no implicit conversion of Symbol into Integer
Run Code Online (Sandbox Code Playgroud)
我的Foo控制器的相关部分如下所示:
def new
@foo = current_user.foos.build
@foo.marflars.build
end
def create
@foo = Foo.new(foo_params)
if @foo.save
redirect_to @foo
else
render action: 'new'
end
end
..
def foo_params
params.require(:foo).permit(:foo_attr, …Run Code Online (Sandbox Code Playgroud) 每当我必须在rails中执行HABTM时,我总是发现自己想知道是否可以从命令行生成所需的迁移.
我希望通过这样做来节省时间:
rails g migration tracks_podcasts tracks:references podcasts:references id:false
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,也没有它的几个变体(有和没有id:false指令)
Railscast 第275集 - 我如何测试使用以下代码向用户发送密码重置:
def send_password_reset
generate_token(:password_reset_token)
....
... etc
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
Run Code Online (Sandbox Code Playgroud)
我的问题是倒数第二行代码:end while User.exists?(column => self[column])它的工作原理很好,但是如果换掉哈希火箭就会导致我的规格失败,即end while User.exists?(column: self[column])
Failure/Error: user.send_password_reset
ActiveRecord::StatementInvalid:
SQLite3::SQLException: no such column: users.column: SELECT 1 FROM "users" WHERE "users"."column" = 'Y7JJV4VAKBbf77zKFVH7RQ' LIMIT 1
Run Code Online (Sandbox Code Playgroud)
为什么会这样?是否有必须使用哈希火箭的情况,是否有任何指导方针?
在我的应用程序中,:foos有很多:bars,我将每个foo序列化为JSON,如下所示:
@foo.as_json(
except: [:created_at, :updated_at],
include: {
bars: { only: [:ip_addr, :active] }
}
)
Run Code Online (Sandbox Code Playgroud)
这给了我以下内容:
{
"id" => 2,
"name" => "Hello World",
"bars" => [
{ "ip_addr" => "192.123.12.32", "active" => 0 },
{ "ip_addr" => "192.123.12.33", "active" => 1 }
]
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我的序列化哈希包含一个不活动的栏。如何从哈希中排除无效的小节?
如果可以的话,那会很棒:
include: { bars: { only: { :active => true }}}
Run Code Online (Sandbox Code Playgroud)
我已经as_json走了很远吗?我现在需要切换到活动模型序列化器吗?
看看我简单的课程:
import sys
class Foo(object):
def __init__(self):
self.frontend_attrs = ['name','ip_address','mode','port','max_conn']
self.backend_attrs = ['name','balance_method','balance_mode']
Run Code Online (Sandbox Code Playgroud)
上面的init方法创建了两个列表,我想动态地引用它们:
def sanity_check_data(self):
self.check_section('frontend')
self.check_section('backend')
def check_section(self, section):
# HERE IS THE DYNAMIC REFERENCE
for attr in ("self.%s_attrs" % section):
print attr
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,python抱怨打电话给("self.%s_attrs" % section).
我读过有关人们使用get_attr动态查找模块的信息......
getattr(sys.modules[__name__], "%s_attrs" % section)()
Run Code Online (Sandbox Code Playgroud)
这可以用于词典.