0

_applicant.html.erb 中的链接在浏览器中如下所示:http://localhost:3000/needs/3/applicants.1 单击此链接时会在浏览器中显示:

Routing Error

No route matches [PUT] "/needs/3/applicants.1"

我希望它更新此特定申请人行的接受列。基本上我希望它将数据发送到申请者控制器的更新方法。我怎样才能修改代码来做到这一点?

_applicant.html.erb

<%= link_to 'Accept Applicant', need_applicants_path(applicant.need_id, applicant.id), :method => :put, :action => "update", :applicant => {:acceptance => true} %>

从运行 rake 路线得到这个:

PUT    /needs/:need_id/applicants/:id(.:format)      applicants#update

路线.rb:

resources :needs, except: [:new] do
 resources :applicants
end

申请人控制器.rb

class ApplicantsController < ApplicationController

  def update
    @need = Need.find(params[:need_id])
    @applicant = @need.applicants.find(params[:id])

    if @applicant.update_attributes(params[:applicant])
      flash[:success] = 'Your applicant has been accepted/rejected!'
      redirect_to @need
    else
        @need = Need.find(params[:need_id])
      render 'needs/show'
    end

  end


end
4

1 回答 1

1

我认为这里有两个可能的修复:

第一的,

http://localhost:3000/needs/3/applicants.1

应该读

http://localhost:3000/needs/3/applicants/1

错误在这一行:

<%= link_to 'Accept Applicant', need_applicants_path(applicant.need_id, applicant.id), :method => :put, :action => "update", :applicant => {:acceptance => true} %>

在哪里...

need_applicants_path(applicant.need_id, applicant.id)

您可以尝试传入两个实例对象,如下所示:

need_applicants_path(Need.find(applicant.need_id), applicant)

其次,另一种可能的解决方案是在您的路线中明确设置 PUT 路径。

在您的 config/routes.rb 添加该行

put 'need/:need_id/applicant/:id/update

然后运行

rake routes

看看 PUT 路径是什么

于 2013-06-18T04:09:01.997 回答