Rails 3如何使用link_to更改d​​b中布尔值的值? [英] Rails 3 how do I use link_to to change the value of a boolean in the db?

查看:117
本文介绍了Rails 3如何使用link_to更改d​​b中布尔值的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a simple link that will allow an admin to approve a user on my website

与此人尝试的非常相似:在Rails中3如何使用button_to更改布尔值?

quite similar to what this guy was trying : In Rails 3 how do I use button_to to change the value of a boolean?

以下是它应该如何工作:

Here's how it was supposed to work :


  1. 管理员点击链接以批准用户

  2. 该链接调用UsersController中的activate函数

  3. 活动函数调用用户模型

  4. 用户模型将已批准属性更新为true并保存

  1. Admin clicks the link to approve a user
  2. The link calls the activate function in UsersController
  3. The active function calls that users model
  4. the user model updates the approved attribute to true and saves it

以及我将如何做到这一点

and here's how I was going to do it

在我的用户中#index view

in my users#index view

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

我的userController中的

in my userController

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

我的用户模型中的

in my user model

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"

现在,当我点击链接(批准用户)时,我已发送回到用户索引页面(就像我应该的那样),但用户字段approved仍然设置为false:我

Now when I click the link (to approve users), I'm sent back to the user index page (like I'm supposed to) but the user field "approved" is still set to false :I

点击后我得到的URL链接:

The URL I get after clicking the link :

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

任何想法?

更新

根据建议iv,将URL添加到link_to帮助程序

As suggested iv added the URL to the link_to helper

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

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

When I click the helper link I get

wrong number of arguments (1 for 2) 

in app / controllers / users_controller.rb:7:在'激活'

in 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])

line 7 where the error is @user.update_attribute(params[:user])

我还应该放在那里?

哦,这是我这个方法的路线

oh and here is my route for this method

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

UPDATE V2
所以我有将更新行更改为:

UPDATE V2 So I have changed that update line to :

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

它似乎做我想做的一切,除了将值更改为true!

and it seems to do everything i want it to do except changing the value to true !

我也试过用1表示true(和update_attribute函数)和非字符串..这里的想法用完了lol

I've also tried to use 1 for true (and the update_attribute function) and non-strings.. running out of ideas here lol

解决方案

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

Well this is awkward but so it happens that in my user model i had attr_accessor :approved this resulted in that the model never went to the database to update the :approved column BUT instead it updated the local variable :approved so next time when I looked at the column then of course the :approved value had not changed

tldr?
如果您的模型中的attr_accessor与您尝试更新的列名称相同=>将其删除

tldr? if you have attr_accessor in your model with the same name as the column your trying to update => remove it

推荐答案

您可以更新以下代码

您查看

<% @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"

希望可以帮到你。

这篇关于Rails 3如何使用link_to更改d​​b中布尔值的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆