Ruby on Rails - 通过链接更新记录

时间:2016-01-10 21:57:01

标签: ruby-on-rails

新手到rails并且一般来说这里编程!我正在进行rails教程,目前在第10章,其中用户模型已基本完成。我正在尝试通过在destroy操作旁边提供指向其他管理员用户的链接来添加新功能以使用户成为管理员用户。

我的链接定义如下:

  <% if current_user.admin? && !current_user?(user) %>  
    | <%= link_to "Make admin", user, method: :makeAdmin,
                                  data: { confirm: "You sure?" } %>
  <% end %>

我的控制器定义:

  def makeAdmin
    @user = User.find(params[:id])
    @user.update_columns(admin_user: true)
    flash[:success] = "Made an admin"
    redirect_to @user
  end

我希望以上工作正常,但是我遇到了路由错误,所以在搜索了stackoverflow后,我对路由进行了一些修改,如下所示:

  resources :users do
    member do
      put 'makeAdmin'
    end
  end

它仍然无效,路由错误仍然存​​在。

有什么想法吗?

rake路线在这里:

                 Prefix Verb   URI Pattern                             Controller#Action
    password_resets_new GET    /password_resets/new(.:format)          password_resets#new
   password_resets_edit GET    /password_resets/edit(.:format)         password_resets#edit
           sessions_new GET    /sessions/new(.:format)                 sessions#new
              users_new GET    /users/new(.:format)                    users#new
                   root GET    /                                       static_pages#home
                   help GET    /help(.:format)                         static_pages#help
                  about GET    /about(.:format)                        static_pages#about
                contact GET    /contact(.:format)                      static_pages#contact
                 signup GET    /signup(.:format)                       users#new
                  login GET    /login(.:format)                        sessions#new
                        POST   /login(.:format)                        sessions#create
                 logout DELETE /logout(.:format)                       sessions#destroy
         makeAdmin_user POST   /users/:id/makeAdmin(.:format)          users#makeAdmin
                  users GET    /users(.:format)                        users#index
                        POST   /users(.:format)                        users#create
               new_user GET    /users/new(.:format)                    users#new
              edit_user GET    /users/:id/edit(.:format)               users#edit
                   user GET    /users/:id(.:format)                    users#show
                        PATCH  /users/:id(.:format)                    users#update
                        PUT    /users/:id(.:format)                    users#update
                        DELETE /users/:id(.:format)                    users#destroy
edit_account_activation GET    /account_activations/:id/edit(.:format) account_activations#edit
        password_resets POST   /password_resets(.:format)              password_resets#create
     new_password_reset GET    /password_resets/new(.:format)          password_resets#new
    edit_password_reset GET    /password_resets/:id/edit(.:format)     password_resets#edit
         password_reset PATCH  /password_resets/:id(.:format)          password_resets#update
                        PUT    /password_resets/:id(.:format)          password_resets#update

2 个答案:

答案 0 :(得分:1)

尝试解决此问题:

<%= link_to "Make admin", make_admin_user_path(user), method: :put,
                              data: { confirm: "You sure?" } %>

答案 1 :(得分:0)

这完成如下!

路线

resources :users do
    member do
      put 'makeadmin'
    end
  end

Controller Def

  def makeadmin
    @user = User.find(params[:id])
    @user.update_attribute :admin, true
    flash[:success] = "this user was made admin"
    redirect_to users_url
  end

链接

| <%= link_to "Make admin", makeadmin_user_path(user), :method=> :put %>

谢谢 http://media.railscasts.com/assets/episodes/videos/035-custom-rest-actions.mp4