首页 > 解决方案 > 更新 ruby​​ 中的单个列

问题描述

`我的去!我在应用程序表中有一个状态列,我将其设置为默认挂起。当用户提交申请时,状态为待处理。现在我被困在这里 1. 我想在经理视图中有两个按钮,即批准和拒绝按钮。2.当经理单击批准按钮时,我想将状态从默认(待定)更新为已批准,拒绝按钮也是如此

here is my controller for applications`

  def index
     @applications = Application.all
   end

  def new
    @application = Application.new

  end

def create
  @application = Application.new(application_params)
  if @application.save
    redirect_to root_path
    flash[:notice] = ' Your application is submitted pending approval'
  else
    render 'new'
  end
end


#my update method
    def update
      @applications = Application.all
      @application = Application.find(params[:id])
      @application.update_attributes(:status => "Approved")
        render 'index'
        flash[:notice] = "Application has been approved"
      end
    # def update
    #   @applications = Application.all
    #   @application = Application.find(params[:id])
    #   @application.update_attributes(:status => "Approved")
    #   render 'index'
    #      flash[:notice] = "Application has been approved"
    #    else
    #      @application.update_attributes(:status => "Rejected")
    #         flash[:notice] = "Application has been rejected"
    # end
  private

    def application_params
      params.require(:application).permit(:name, :phone_number, :email, :gender, :date_of_birth, :course, :address, :status)
    end

end```
`my views`
  ```<td><%= link_to "Approve", application_path(application.id), method: :put, :class => 'mb-sm btn btn-green' %></td>
                <td><%= link_to "Reject", application_path(application.id), method: :put, :class => 'mb-sm btn btn-danger' %></<td>

and my routes
 Rails.application.routes.draw do
   get 'manager/index'
   get 'about/index'

  resources :contacts
  resources :registers
  resources :forms
  resources :applications

  # put 'applications/update'

  root to: "landing#index"

  devise_for :managers, path: 'managers', controllers: {sessions: "managers/sessions"}
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
      devise_for :users, path: 'users',  controllers: {sessions: "users/sessions"}
    end```
any one kindly help me


标签: ruby-on-rails

解决方案


Mutebi,有不止一种方法可以做到这一点。一个易于理解的解决方案是实现一个控制器操作,用于将状态设置为 :accepted,另一个用于将状态设置为 :rejected。

在您的控制器中:

# app/controllers/applications_controller.rb
def accept
  @application = Application.find params[:id]
  @application.status = :accepted
  @application.save
  # add more code as needed,
  # then render something
end

def reject
  @application = Application.find params[:id]
  @application.status = :rejected
  @application.save
  # add more code as needed,
  # then render something
end

然后,创建相应的路由:

# config/routes.rb
ressources :applications do
  member do 
    put 'accept'
    put 'reject'
  end
end

这将创建以下命名路由

accept_application_path(@application)
reject_application_path(@application)

您可以使用它在视图中创建请求的链接:

# app/views/applications/_application.html
link_to "Approve", approve_application_path(@application), method: :put
link_to "Reject", reject_application_path(@application), method: :put

我必须说我没有测试我的解决方案,但除了偶尔的错字,它应该可以工作。如果没有,请告诉我。具体来说,您可能需要从@application 中删除@,具体取决于您在视图中拥有的变量类型。从您的控制器来看,它看起来像一个实例变量,但在您的视图中,您使用的是局部变量。确保这不会导致错误。


推荐阅读