【问题标题】:Rails no route matches updateRails 没有路线匹配更新
【发布时间】:2017-04-21 14:17:59
【问题描述】:
# 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 =&gt; 1@query =&gt; Query.find_by_id(1)@query =&gt; Query.find(params[:query_id]) 传递给部分,但仍然收到“没有路由匹配更新”错误。

我试过&lt;%= form_for(@query) do |f| %&gt;而不是url方式,但得到这个错误QueriesController#MY_PARTIAL is missing a template for this request format and variant.

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

【问题讨论】:

  • &lt;%= form_for @query do |f| %&gt; 通常只要在您的路线中设置正确就足够了。
  • @Iceman 是的,当这不起作用时我很惊讶。我正在提交form_for 来自已正确链接到控制器的关注文件深处的路由,所以我认为可能需要更多规范?
  • 你是如何渲染这个局部的?
  • $("#query-builder").hide().html("&lt;%= escape_javascript(render 'queries/layer_5') %&gt;").fadeIn(500);这个js是通过提交form_tag触发的
  • @Iceman 已解决/已编辑。感谢您的帮助!

标签: ruby-on-rails forms routes


【解决方案1】:

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

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

这现在适用于&lt;%= form_for(@query) %&gt;

【讨论】:

  • 你可以Query.find(params[:id]),默认使用id
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2012-10-11
  • 2017-07-13
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多