在rails中编辑对象时没有这样的路由

时间:2014-12-19 21:44:55

标签: ruby-on-rails ruby-on-rails-4

我有一个对象,我试图允许用户在我的rails 4 app中编辑。用户has_one supp_form,我希望他们能够编辑supp_form中的信息。页面加载正常,关系设置正确。

错误

No route matches [PATCH] "/businesses/3/supp_form/edit"

当我耙路线时,我看到以下路线:

edit_business_supp_form_path    GET /businesses/:business_id/supp_form/edit(.:format)   supp_forms#edit
GET /businesses/:business_id/supp_form(.:format)    supp_forms#show
PATCH   /businesses/:business_id/supp_form(.:format)    supp_forms#update
PUT /businesses/:business_id/supp_form(.:format)    supp_forms#update

supp_forms_controller.rb

class SuppFormsController < ApplicationController
    before_filter :authenticate_user!

    def new
      @suppform = SuppForm.new(supp_form_params)
    end

    def create
      @suppform = SuppForm.create(supp_form_params)
    end

    def edit
      @user = User.current_user
      @suppform = @user.supp_form
    end 

    def update
      @user = current_user
      @suppform = SuppForm.update(supp_form_params)
    end 

    private

        def supp_form_params
          params.require(:supp_form).permit(:id, :business_id, :title, :first_name, 
            :last_name, :applicant_role, :work_phone_number)
        end
end

查看

<%= form_for @user.supp_form, :url => edit_business_supp_form_path(@user.supp_form), :html => { :class => "sky-form", :id => "sky-form4" } do |supp_form| %>
  <%= supp_form.text_field :work_phone_number, :placeholder => "Your new phone number"  %>
<% end %>

1 个答案:

答案 0 :(得分:1)

问题是它尝试使用PATCH请求访问路由,该请求用于更新。在您的路由中,/businesses/:business_id/supp_form/edit路由仅为GET请求指定,因此错误 发生这种情况是因为您在表单中使用的路径指向edit操作(仅负责显示编辑表单),而应指向update操作。因此,您应该实际使用的路由是supp_form_path,它与PATCH方法一起将信息推送到更新操作,更新对象。