我正在尝试创建一个简单的链接,允许管理员批准我的网站上的用户
与这个家伙的尝试非常相似:在Rails 3中如何使用button_to来改变布尔值?
这是它应该如何工作:
这就是我要做的事情
在我的用户#index视图中
<% @users.each do |user| %>
<%= link_to 'Approve', :action => "index", :method => 'activate', :id => user.id, :class => 'btn btn-mini btn-danger' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在我的userController中
def activate
@user = User.find(params[:user])
@user.activate_user
end
Run Code Online (Sandbox Code Playgroud)
在我的用户模型中
def activate_user
self.approved = 'true'
self.save
end
Run Code Online (Sandbox Code Playgroud)
和我的路线
devise_for :users, :controllers => { :registrations => "registrations" }
resources :users do
member do
get 'activate'
put 'activate'
end
end
match "users/:id/activate" => "users#activate"
Run Code Online (Sandbox Code Playgroud)
现在当我点击链接(批准用户)时,我被发送回用户索引页面(就像我应该的那样),但用户字段"approved"仍然设置为false:I
点击链接后我得到的网址: …
methods model-view-controller routes ruby-on-rails hyperlink