【问题标题】:I am anticipating a patch request but am receiving a post instead我期待一个补丁请求,但我收到了一个帖子
【发布时间】:2018-08-25 20:06:34
【问题描述】:

我在我的 edit.html.erb 上提交表单,它给了我一个错误,因为没有路由匹配 [POST]“/grouponepostings/17/edit”。我在我的文档中使用了简单的表单,但不确定如何指定它是我寻求的补丁请求。

这里是 routes.rb

Rails.application.routes.draw do
  get "/", to: "pages#index"
  get "/grouponepostings", to:"grouponepostings#index"
  get '/grouponepostings/new', to: 'grouponepostings#new'
  get "/grouponepostings/:id", to:"grouponepostings#show"
  post '/grouponepages', to: 'grouponepostings#create'
  get '/grouponepostings/:id/edit', to: 'grouponepostings#edit'
  patch 'grouponepostings/:id/edit', to: 'grouponepostings#update'
end

这里edit.html.erb:

<%= simple_form_for :postings1314 do |r| %>
  <div>
    <%=r.label :firstName%>
    <%=r.text_field :firstName%>
  </div>

  <div>
    <%=r.label :lastName%>
    <%=r.text_field :lastName%>
  </div>

  <div>
    <%=r.label :age%>
    <%=r.text_field :age%>
  </div>

  <div>
    <%=r.label :bio%>
    <%=r.text_field :bio%>
  </div>

  <div>
    <%= r.submit %>
  </div>
<%end%>

这里是 grouponepostings_controller.rb:

class GrouponepostingsController < ApplicationController
  def index
    @postings1314 = Grouponepage.all
  end
  def show
    @posting1314singular = Grouponepage.find(params[:id])
  end
  def new
    @postings1314 = Grouponepage.new
  end
  def create
    page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    @posting1314 = Grouponepage.new(page_params)
    @posting1314.save
    redirect_to '/grouponepostings'
  end
  def edit
    @posting1314 = Grouponepage.find(params[:id])
  end
  def update
    @posting1314 = Grouponepage.find(params[:id])
    page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    @posting1314.update(page_params)
    redirect_to '/'
  end
end

【问题讨论】:

标签: ruby-on-rails ruby forms post http-patch


【解决方案1】:

改变路线

Rails.application.routes.draw do
 resources :grouponepostings # This will generate REST routes
 root to: "pages#index" # This should be the last route
end

more

【讨论】:

  • 这使我的 routes.rb 更短,但并没有修复我的语法错误。错误信息是一样的。
  • simple_form_for :postings1314更改为simple_form_for @postings1314
【解决方案2】:

在 routes.rb 上:

patch 'grouponepostings/:id/edit', to: 'grouponepostings#update', as: :update_posting

在edit.html.erb上:

<%= simple_form_for @postings1314, url: update_posting_path, method: :patch do |r| %>

请注意,我还添加了@@postings1314

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多