2010-05-14 72 views
0

我在尝试使用Super Inplace Controls插件,它有一个in_place_select方法。我有以下型号:Rails:in_place_select设置外键

class Incident < ActiveRecord::Base 
    belongs_to    :incident_status 
    validates_existence_of :incident_status 
end 

class IncidentStatus < ActiveRecord::Base 
    has_many :incidents 
end 

现在,IncidentStatus只是一个ID和名称('打开'和'关闭')。在事件显示视图中,我想让用户点击当前状态,并使用选择菜单对其进行更改。因此,在show.html.erb,我有以下几点:

<p> 
    <b>Status:</b> 
    <%= in_place_select :incident, :incident_status_id, :choices => @statuses.map { |e| [e.name, e.id] }, :display_text => @status.name %> 
</p> 

这是继在中超就地给出的例子很紧密控制的文件,这就是:

<%= in_place_select :employee, :manager_id, :choices => Manager.find_all.map { |e| [e.name, e.id] } %> 

这其实正常工作,直到我点击确定更改字段并提交POST。我看到状态,然后当我点击它时,会弹出一个包含“打开”和“关闭”的下拉菜单。当我选择“关闭”并按下确定时,表单消失。下面以Web服务器控制台显示出来:

Processing IncidentsController#set_incident_incident_status_id (for 127.0.0.1 at 2010-05-14 10:18:43) [POST] 
    Parameters: {"commit"=>"OK", "action"=>"set_incident_incident_status_id", "authenticity_token"=>"eahXrzwJwe+h2Byi1ELWXLy0QNmqF2EEXNw+eAfUJwU=", "id"=>"1", "controller"=>"incidents", "incident"=>{"incident_status_id"=>"1"}} 

...snip... 

ActionController::UnknownAction (No action responded to set_incident_incident_status_id. Actions: admin?, authenticated?, authorized?, create, destroy, edit, index, new, set_incident_incident_status, set_incident_title, show, and update): 

所以,这似乎是在寻找一种方法“set_incident_incident_status_id”。如果我在我的事件控制器中定义了这个方法,它似乎没问题,但我不知道如何获取传入的“incident_status_id”=>“1”并将其设置为外键。

任何想法?提前致谢!

回答

0

终于明白了。我需要在事件控制器中创建一个方法。它看起来像这样:

def set_incident_incident_status_id 
    @incident = @customer.incidents.find(params[:id]) 
    @incident.incident_status = IncidentStatus.find(params[:incident][:incident_status_id]) 
    @incident.save 
end 

我需要完成它,但这是主旨。