Rails Cancan:在注册时定义默认角色

我最近使用CanCanCan(通过枚举)为我的rails应用程序添加了角色 – 但现在我想在注册时添加默认角色。 我该怎么做呢? 它是在控制器还是模型中? 我的用户型号: class User < ActiveRecord::Base #Defining different roles enum role: [:Admin, :User, :Guest] #Users can only create one scholarship application has_one :applications # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end 我的能力模型 – 只有三个角色,一个管理员,我将通过为一个角色为1的用户播种数据库来创建,然后其他人应该在注册时为2: class Ability include […]

有很多通过两个模型

我有两个重要的模型。 还有更多的模型与它们相互作用,但只有这些模型应该是相关的。 class Image < ActiveRecord::Base belongs_to :user belongs_to :trip end class Mission < ActiveRecord::Base belongs_to :user belongs_to :trip # has_many :images, through: :user end 注释掉的线路让我在那里中途,但我希望满足图像的附加条件,其具有与任务相同的行程ID。 # has_many :images, through: [:user, :trip] 使用类似于某些类似方法的数组有时会采用无效语法。 def images Image.where(user_id: user_id, trip_id: trip_id) end 我应该这样做吗? 或者有更好的方法吗? 我也试过了has_many上的条件,但是我放在那里的任何动态都被调用了Image::ActiveRecord_Relation 以下是一些示例记录: 所以Mission.find(1).images给出了3个图像,而2和3给出了一个空数组。

失去了validation失败的思维,保存了嵌套模型

没头绪。 一直在阅读文档和示例,并没有能够弄清楚这一点。 我可能错过了一些非常明显的东西。 楷模 class Item :destroy accepts_nested_attributes_for :item_ownerships validates :collection, :presence => true end class ItemOwnership true validates :item_id, :presence => true end class User < ActiveRecord::Base has_many :item_ownerships end 调节器 class ItemsController :toggle_item_owned_state) do @collection = current_user.collections.find(params[:collection_id]) end def new @item = Item.new collection_id: @collection.id @item_ownership = @item.item_ownerships.build(:owned => true, :user => current_user, […]

通过新表单传递ID

这个问题是关于我正在尝试的另一种方法: 通过新表单传递ID 我有一个group#view page ,由Person访问。 在此页面中, Person可以通过我开发的方法查看组的成员。 问题是我需要使用group的Id,访问页面的person的id以及来自该group的id来创建模型Honors 。 在我的Honors控制器中,我有: def create @person = Person.find(current_person) @asked_groupmembership = @person.owned_group_memberships.find_all_by_status(true,:include => [:group, :member]) @asked_groupmembership.each do |agm| @honor = Honor.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person), :honored => Person.find(agm.member.id)) end if @honor.save … end 在我看来,我有一个链接,指导person到表单,以创建一个新的荣誉: @group.id, :person => current_person.id, :honored => agm.member.id) %> 但在我的表格中,我无法得到ids和东西 honors_path(:group_id, :person, :honored)) do |f| […]

如何使用jquery无法正常工作来限制嵌套表单字段

我正在关注嵌套表单字段中的Ryan Bates剧集,并添加了第2部分末尾建议的jquery。一切正常(我可以添加字段和删除字段)。 我现在想限制您可以在表单中添加的字段数。 在我的application.js我有 function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp(“new_” + association, “g”) $(link).parent().before(content.replace(regexp, new_id)); } 正如莱恩贝茨写的那样。 阅读另一篇文章我改变了现在的行: function add_fields(link, association, content) { if($(“.fields input”).length < 5) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).parent().before(content.replace(regexp, new_id)); } } 然而,这不起作用,我在这里做错了什么。 […]

Rails – 模型疑问

鉴于我有这样的模型: class Person has_many :owned_groups, :class_name => “Group”, :foreign_key => :owner_id has_many :owned_group_memberships, :through => :owned_groups, :source => :group_memberships has_many :group_memberships, :foreign_key => “member_id” has_many :groups, :through => :group_memberships end class GroupMembership belongs_to :member, :class_name => ‘Person’ belongs_to :group end class Group belongs_to :owner, :class_name => “Person” has_many :group_memberships has_many :members, :through => :group_memberships end […]

Rails 4关系问题扩展

与此处发布的此问题类似,但我不确定该解决方案是否适用。 Rails 4关系问题,登录时 我在localhost:3000中运行设置时不断收到此消息。 错误:关系“XXX”不存在 第1行:选择DISTINCT“XXX”。*从“XXX”INNER JOIN …. 我希望(希望)理解这个错误是如何产生的,需要别人的帮助来建议如何排除故障。 请随时告诉我,我需要将哪部分代码粘贴到此处以供您理解。

Rails 4,has_many通过关联 – 查找关联的对象

我想知道为什么Item.where(category: x)不适合我。 我希望这个语句能够返回分类在类别x下的所有项目。 请看下面我的协会: class Category :categorizations end class Item :categorizations end class Categorization < ActiveRecord::Base belongs_to :item belongs_to :category end Category.find(1).items返回该类别的所有项目。 请参阅下面的byebug控制台输出,它进一步说明了我的观点。 看起来生成的SQl正在寻找Item表中的类别列 – 当然不存在。 有任何想法吗? 1: class ItemsController 6: end 7: (byebug) Item.where(category: 1) Item Load (0.6ms) SELECT “items”.* FROM “items” WHERE “items”.”category” = 1 # (byebug) Category.find(1).items Category Load (0.7ms) SELECT “categories”.* […]

如何从omniauth获取facebook的时区?

user.rb中的所有内容在注册时正确传递,时区除外。 我无法弄清楚为什么我在尝试登录时遇到此错误 : 2016-03-30T20:13:38.083469+00:00 app[web.1]: NoMethodError (undefined method `timezone=’ for #): 2016-03-30T20:13:38.083470+00:00 app[web.1]: app/models/user.rb:72:in `block in from_omniauth’ 2016-03-30T20:13:38.083471+00:00 app[web.1]: app/models/user.rb:66:in `tap’ 2016-03-30T20:13:38.083471+00:00 app[web.1]: app/models/user.rb:66:in `from_omniauth’ 2016-03-30T20:13:38.083472+00:00 app[web.1]: app/controllers/sessions_controller.rb:7:in `facebook’ user.rb def self.from_omniauth(auth) # Sets 60 day auth token oauth = Koala::Facebook::OAuth.new(“1540371223342976229929”, “ee917abf2e8f1c98274cd323fa1234ebb1346f4”) # Fake Numbers new_access_info = oauth.exchange_access_token_info auth.credentials.token new_access_token = new_access_info[“access_token”] new_access_expires_at = DateTime.now […]

Rails帮助构建管理区域 – 路由问题

我正在建立一个管理区域。 在app / controllers中 application_controller.rb public_controller.rb admin_controller.rb kategoris_controller.rb 在app / controllers / admin中 kategoris_controller.rb 我的路线档案: resources :kategoris namespace :admin do resources :kategoris end 我的kategori索引文件在app / views / admin / kategories / index中 Listing kategoris Navn ‘Are you sure?’, :method => :delete %> 我的kategori控制器在Admin / kategoris_controller.rb中 class Admin::KategorisController @kategoris } end end # GET /kategoris/1 # […]