小编Hab*_*bax的帖子

如何使用Rails 3在post/put simple_form中传递get参数?

使用simple_form 2.0.4,我做:

= simple_form_for(@thing) do |f|
Run Code Online (Sandbox Code Playgroud)

它产生:

<form accept-charset="UTF-8" action="/things/37" class="simple_form" id="edit_thing_37" method="post" novalidate="novalidate">
Run Code Online (Sandbox Code Playgroud)

但是我需要:

<form accept-charset="UTF-8" action="/things/37?foo=bar" class="simple_form" id="edit_thing_37" method="post" novalidate="novalidate">
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

谢谢!

ruby-on-rails-3 simple-form

5
推荐指数
1
解决办法
5726
查看次数

具有Devise的多租户(acts_as_tenant)(用户默认为子域的作用域)会中断会话

宝石

ruby 1.9.3
rails 3.2.11
devise 2.2.3
acts_as_tenant 0.2.9
Run Code Online (Sandbox Code Playgroud)

我的所有模型都是由domain_id限定的:

class User < ActiveRecord::Base
  acts_as_tenant(:domain)
  #...
end
Run Code Online (Sandbox Code Playgroud)

然后,在我的application_controller中,我从域中设置当前租户:

class ApplicationController < ActionController::Base
  set_current_tenant_through_filter
  before_filter :set_tenant
  protect_from_forgery
  #...

  def set_tenant
    #...
    @domain = Domain.find_or_create_by_name(request.host)
    set_current_tenant(@domain)
  end
end
Run Code Online (Sandbox Code Playgroud)

一切都适用于除会话之外的所有模型:每次加载页面时,它都会注销第一个用另一个租户加载页面的用户.通过加载此页面,它将注销[...等]的第一个用户

假设:当Alice访问域时,Rails加载current_tenant = alice_domain(ok).所有工作都按预期工作,直到Bob访问另一个域,加载current_tenant = bob_domain.当Alice刷新她的页面时,Rails仍然有current_tenant == bob_domain.Rails检查会话:Alice与bob_domain范围不存在,因此Devise强制Alice注销.然后application_controller设置current_tenant = alice_domain ...注销Bob.

肮脏的解决方法:不要在用户模型中使用acts_as_tenant,在每个控制器中按域自己定位用户,然后覆盖设计范围登录和按域注册.而且我不确定如何让Devise知道会话中的当前域名.顺便说一句,用户手动default_scope替换acts_as_tenant属于同样的奇怪错误.走这条路似乎很脏.

我正在寻找一个干净的解决方案好几天.我会非常感谢任何帮助.

session scope multi-tenant devise ruby-on-rails-3

5
推荐指数
1
解决办法
1989
查看次数

Rails 3,具有lambda条件的has_one/has_many

我的模特在这里:

class User < ActiveRecord::Base
  has_many :bookmarks
end

class Topic < ActiveRecord::Base
  has_many :bookmarks
end

class Bookmark < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic
  attr_accessible :position
  validates_uniqueness_of :user_id, :scope => :topic_id
end
Run Code Online (Sandbox Code Playgroud)

我想topicscurrent_user相关的for 来获取所有内容bookmark.ATM,我这样做:

Topic.all.each do |t|
    bookmark = t.bookmarks.where(user_id: current_user.id).last
    puts bookmark.position if bookmark
    puts t.name
end
Run Code Online (Sandbox Code Playgroud)

这很难看并且做了太多的数据库查询.我想要这样的东西:

class Topic < ActiveRecord::Base
  has_one :bookmark, :conditions => lambda {|u| "bookmarks.user_id = #{u.id}"}
end

Topic.includes(:bookmark, current_user).all.each do |t| # this must also includes topics without bookmark …
Run Code Online (Sandbox Code Playgroud)

ruby lambda activerecord ruby-on-rails ruby-on-rails-3

2
推荐指数
1
解决办法
5115
查看次数