1

我在文件中有link_to一行代码html.erb

<%= link_to "no thanks" %>

我想点击这个链接来触发:reject我在这个“treating.rb”模型文件中设置的状态方法:

class Treating < ActiveRecord::Base
  attr_accessible :intro, :proposed_date, :proposed_location, :requestee_id, :state

  state_machine :state, :initial => :pending do

    event :reject do
        transition [:pending] => :rejected
    end

    event :reply do
        transition [:pending, :rejected] => :replied
    end

    event :archive do
      transition [:rejected] => :archived
    end
  end
  ...
end

我在我link_to的代码行中添加了什么来获得它所指的“治疗”,以使其状态从“待定”变为“拒绝”?我已经尝试过action =>method =>但没有成功。

4

1 回答 1

2

您需要一个控制器操作来执行此操作,例如:

# ?.html.erb
<%= link_to "no thanks", reject_treating_path(@treating), method: :post %>

# config/routes.rb
resources :treatings do
  member do
    post :reject
  end
end

# app/controllers/treatings.rb
class TreatingsController < ApplicationController
  # POST /treatings/:id/reject
  def reject
    current_user.treatings.find(params[:id]).reject!
    redirect_to treatings_path, notice: "Treating was rejected"
  end
end

我为处理资源添加了一个成员操作,并假设处理属于用户。

于 2012-08-10T23:44:04.673 回答