我正在通过Micheal的学习Ruby on Rails。
在我的web应用程序中,我需要两种类型的用户,称为发布者和订阅者。目前我有一个发布者资源和一个订阅者资源。我希望他们两个都能够使用会话资源进行登录/注销。
我正在寻找一些关于如何实现这一点的方向。我的发布者和订阅者资源是否应该从用户资源继承,这样,例如,下面的代码就可以通过多态性工作?
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:pass
我有以下几点:
链接模型
一个LinkItem模型,我想成为以下类型的
评论
标签
..。
我正在使用以下代码:
链路模型
class Link < ActiveRecord::Base
has_many :link_items
end
LinkItem模型
class LinkItem < ActiveRecord::Base
belongs_to :link
end
class Comment < LinkItem
end
class Tag < LinkItem
end
现在我不知道如何告诉Rails我的L
我有一个名为Attachment的多态模型。我正在使用gem Carrierwave保存附件。
在我的Customer编辑页面上,我执行以下代码:
puts @customer.attachments.count
@customer.attachments.each do |i|
puts i.id #outputs a blank line
end
puts @customer.attachments.count输出0。但是,迭代器仍然在附件上运行1次,并打印出一个空行来代替puts i.id。
这是我的模型:
class Attachment &l
我正在回顾关联,并讨论了这篇关于多态关联的文章。在本文中,它有以下代码:
# Without polymorphism
class Post
belongs_to :person
belongs to :group
end
class Person
has_many :posts
end
class Group
has_many :posts
end
这篇文章对上述代码做了如下说明
通常,为了找出谁拥有某个个人资料,我们会查看列,即foreign_key。foreign_key是用于在相关模型的表中查找相关对象的id。
但是,我们的Posts表将有两个相互竞争的外
我从我的一个旧的ruby on rails 3应用程序中借用了一些代码来开发一个新的rails 4应用程序。代码可以在旧站点上运行,但在新站点上就不行了。
这是我的routes.rb:
scope ':username' do
resources :recipes
end
get "/:username" => "recipes#index"
这是我的控制器索引:
def index
@user = User.find_by_username params[:username]
@recipes = Recipe.all
e
我有以下代码:
class User < ActiveRecord::Base
has_many :campaigns
...
end
class Campaign < ActiveRecord::Base
belongs_to :user
...
end
class CampaignsController < ApplicationController
def create
@campaign = Campaign.new(campaign_params)
@campaign.user_id = @current_user.id
我是Rails的新手,目前正试图为我做一个小的测试网站。我的代码中有一个问题,它声明了一个“未定义的方法`service_providers‘for #”--产生错误的代码行如下:
def new
@service_provider = current_user.service_providers.build(serviceprovider_params)
end
我的数据库模型是表“has_one "ServiceProvider”has_many“服务。我使用ruby宝石“设计”作为用户模型。我试图在我的示例应用程序中传递“Rails教程”(Rails)微贴的想法。在清单1
我在访问"belongs_to“关联中的模型时遇到了困难。我有“帖子”和“评论”模式,每个模式都可以有很多“喜欢”。现在,我可以
comment.likes
或
post.likes
但是如果我尝试的话,我会得到一个“未定义的方法”
like.comment
我试图按以下方式实现Rails协会:
class Post < ActiveRecord::Base
include Likeable
....
class Comment < ActiveRecord::Base
include Likeable
我有一个“讨人喜欢”的模块:
mo