Rails没有路由匹配更新

时间:2017-04-21 14:17:59

标签: ruby-on-rails forms routes

# Here's the error I am getting when trying to load my partial:
`ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"queries"}:`


# Here's the form I am trying to load within a partial on the `queries/1/edit` page:
`<%= form_for @query, url: {action: 'update'}, method: :put do |f| %>`


# Here's my controller:
def update
  @query = Query.find(params[:query])
  if @query.update(query_params)
    redirect_to queries_path, notice: 'Saved query successfully!'
  else
    redirect_to edit_query_path, alert: @query.errors.full_messages
  end
end


# Route:
resources :queries

我尝试将@query => 1@query => Query.find_by_id(1)以及@query => Query.find(params[:query_id])传递给部分,但仍然会收到“无路由匹配更新”错误。

我尝试了<%= form_for(@query) do |f| %>而非网址方式,但收到此错误QueriesController#MY_PARTIAL is missing a template for this request format and variant.

请帮我解开=)Rails 5.0.1, ruby 2.3.0p0

1 个答案:

答案 0 :(得分:1)

所以我再次看了form_for params,令我惊讶的是,我看到id: "1"被传递了。因此,我将控制器更改为

def update 
  @query = Query.find_by_id(params[:id])

现在可以使用<%= form_for(@query) %>