【问题标题】:Rails 3 how do I use link_to to change the value of a boolean in the db?Rails 3 如何使用 link_to 更改数据库中布尔值的值?
【发布时间】:2012-10-22 08:52:59
【问题描述】:

我正在尝试创建一个简单的链接,允许管理员在我的网站上批准用户

与这个人的尝试非常相似:In Rails 3 how do I use button_to to change the value of a boolean?

它应该是这样工作的:

  1. 管理员单击链接以批准用户
  2. 链接调用UsersController中的activate函数
  3. 用户建模的活动函数调用
  4. 用户模型将approved属性更新为true并保存

这就是我打算这样做的方式

在我的用户#index 视图中

 <% @users.each do |user| %> 
 <%= link_to 'Approve', :action => "index", :method => 'activate', :id => user.id, :class => 'btn btn-mini btn-danger' %> 
<% end %> 

在我的用户控制器中

def activate
  @user = User.find(params[:user])
  @user.activate_user
end

在我的用户模型中

def activate_user 
  self.approved = 'true'
  self.save
end

还有我的路线

devise_for :users, :controllers => { :registrations => "registrations" }

resources :users do
  member do
    get 'activate'
    put 'activate'
  end
end

match "users/:id/activate" => "users#activate"

现在,当我单击链接(批准用户)时,我会返回到用户索引页面(就像我应该这样做的那样),但用户字段“已批准”仍设置为 false:I

点击链接后的网址:

  http://localhost:3000/users?class=btn+btn-mini+btn-danger&id=2&method=activate

有什么想法吗?

更新

按照建议 iv 将 URL 添加到 link_to 帮助程序

<% @users.each do |user| %> 
  <%= link_to "Approve", :controller => "users", :id => user.id, :action => "activate", :method => :put, :approved => true %> 
<% end %>

当我点击帮助链接时,我得到了

wrong number of arguments (1 for 2) 

在 app/controllers/users_controller.rb:7:in `activate'

def activate
  @user = User.find(params[:id])
  @user.update_attribute(params[:user])
  redirect_to "/users?approved=false"
end

错误所在的第 7 行 @user.update_attribute(params[:user])

我还应该放什么?

哦,这是我使用这种方法的路线

match "/users/:id/activate" => "users#activate"

更新 V2 所以我已将该更新行更改为:

@user.update_attributes(@user.approved, "true")

它似乎做了我想做的所有事情,除了将值更改为 true !

我也尝试将 1 用于 true(以及 update_attribute 函数)和非字符串。。这里的想法用完了,哈哈

解决方案

这很尴尬,但碰巧在我的用户模型中我有attr_accessor :approved 这导致模型从未进入数据库更新:approved 列但它更新了局部变量:approved 所以下次当我查看该列时,:approved 的值当然没有改变

tldr? 如果您的模型中有 attr_accessor 与您尝试更新的列同名 => 删除它

【问题讨论】:

  • link_to的第二个参数应该是一个url,然后你输入你的选项。试试link_to "Approve", users_path, #...
  • Tru 运行 rake 路由并查看哪条路由实际指向 users#activate。使用辅助变量。

标签: ruby-on-rails model-view-controller methods routes hyperlink


【解决方案1】:

看起来您没有正确调用 link_to。请注意,您将操作作为 http 参数,而不是 URL 的一部分。

网址应该是

http://localhost:3000/users/123/activate

查看此页面上的示例:

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

我认为你需要使用 :controller => 'users', :action => 'activate'。

请记住,您也可以在此处使用字符串作为路径:"/users/${@user.id}/activate"

【讨论】:

  • 非常感谢您的链接!阅读 API 后,我不确定 link_to 是否是正确的方法。我想要的只是一个按钮,它在用户控制器中调用“激活”函数,使批准的属性为真,然后返回用户索引,我应该使用 link_to 吗?
  • 也许按钮标签会更好。见stackoverflow.com/questions/2254720/…
  • 我想我可以让 link_to 工作,你对丢失的 url 的看法是对的,但它导致我出现新错误,请参阅我更新的帖子 :)
【解决方案2】:

您可以更新下面的代码

你看

<% @users.each do |user| %> 
<%= link_to 'Approve', active_user_path(user) %> 
<% end %>

你的控制者

def activate
  @user = User.find(params[:id])
  if @user.update_attribute(:approved, true)
    redirect_to "something"
  else
   render "something"
  end 
end

你的路线

match "users/:id/activate" => "users#activate", :as => "active_user"

希望能帮到你。

【讨论】:

  • 我做了您建议的更改,但我仍然收到此错误 wrong number of arguments (1 for 2) 这条线 @user.update_attribute(approved: true)
  • 好吧,这太奇怪了:它可以更改 :email 但不能更改 :approved 属性,我使用 sqlite 有关系吗?它似乎更喜欢使用 't' 而不是 true (但数据库中的值仍然没有变化)
  • 似乎 Rails 和 Sqlite 在布尔值方面不能很好地沟通!我需要某种适配器,但你已经解决了我的老问题,谢谢! :)
  • @Matti update_attribute 方法接受两个参数,而不是一个。您正在传递一个哈希,这是一个单一的参数。请查看here 以获得更详细的说明。
【解决方案3】:

attr_accessor 方法将创建一个实例变量以及该变量的 getter 和 setter。

当您尝试使用该名称更新数据库列时,它会改用实例变量。如果您想拥有一个“虚拟列”,这将很有用,它将充当实际列但存储在内存中。

对于数据库列,您有 attr_accessible 允许对该列进行批量分配(这将在 rails 4 中被弃用),或者您可以不使用任何方法来定义列的可访问性并让 ActiveRecord 处理它。

【讨论】:

    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 2011-10-30
    • 2015-10-07
    • 2017-08-05
    • 1970-01-01
    • 2021-11-15
    • 2011-05-31
    • 2014-06-03
    相关资源
    最近更新 更多