【问题标题】:ActionController::UrlGenerationError in Products#edit产品中的 ActionController::UrlGenerationError#edit
【发布时间】:2014-09-12 18:34:22
【问题描述】:

我是 Rails 新手,在销售网站上遇到问题,我有一个用户和 protuto 用户所属的 produito,一切正常 exe 以编辑产品的操作。

产品中的ActionController::UrlGenerationError#edit

没有路由匹配 {:action=>"show", :controller=>"products", :format=>nil, :id=>nil, :user_id=>#} 缺少必需的键:[:id]

my_edit
    <h1>Editing product</h1>

    <%= render 'form' %>

    <%= link_to 'Show', @product %> |
    <%= link_to 'Back', products_path %>

my _form

    <%= form_for  [:user,@product], :html => {multipart: true} do |f| %>
      <% if @product.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

          <ul>
          <% @product.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="field">
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      </div>
      <div class="field">
        <%= f.label :price %><br>
        <%= f.text_field :price %>
      </div>
      <div class="field">
        <%= f.label :local %><br>
        <%= f.text_field :local %>
      </div>
      <div class="field">
        <%= f.label :description %><br>
        <%= f.text_area :description %>
      </div>
      <div class="field">
        <%= f.label :category_id %><br>
        <%= f.select :category_id, Category.all.map {|c| [c.name, c.id]} %>
      </div>
      <div class="field">
        <%= f.label :contacts %><br>
        <%= f.text_field :contacts %>
      </div>
      <div class="field">
        <%= f.label :image %><br>
        <%= f.file_field :image %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>


my rake routes

 Prefix Verb   URI Pattern                                 Controller#Action
     sessions_new GET    /sessions/new(.:format)                     sessions#new
category_category GET    /categories/:id/category(.:format)          categories#category
       categories GET    /categories(.:format)                       categories#index
                  POST   /categories(.:format)                       categories#create
     new_category GET    /categories/new(.:format)                   categories#new
    edit_category GET    /categories/:id/edit(.:format)              categories#edit
         category GET    /categories/:id(.:format)                   categories#show
                  PATCH  /categories/:id(.:format)                   categories#update
                  PUT    /categories/:id(.:format)                   categories#update
                  DELETE /categories/:id(.:format)                   categories#destroy
       home_index GET    /home(.:format)                             home#index
                  POST   /home(.:format)                             home#create
         new_home GET    /home/new(.:format)                         home#new
        edit_home GET    /home/:id/edit(.:format)                    home#edit
             home GET    /home/:id(.:format)                         home#show
                  PATCH  /home/:id(.:format)                         home#update
                  PUT    /home/:id(.:format)                         home#update
                  DELETE /home/:id(.:format)                         home#destroy
     profile_user GET    /users/:id/profile(.:format)                users#profile
    user_products GET    /users/:user_id/products(.:format)          products#index
                  POST   /users/:user_id/products(.:format)          products#create
 new_user_product GET    /users/:user_id/products/new(.:format)      products#new
edit_user_product GET    /users/:user_id/products/:id/edit(.:format) products#edit
     user_product GET    /users/:user_id/products/:id(.:format)      products#show
                  PATCH  /users/:user_id/products/:id(.:format)      products#update
                  PUT    /users/:user_id/products/:id(.:format)      products#update
                  DELETE /users/:user_id/products/:id(.:format)      products#destroy
            users GET    /users(.:format)                            users#index
                  POST   /users(.:format)                            users#create
         new_user GET    /users/new(.:format)                        users#new
        edit_user GET    /users/:id/edit(.:format)                   users#edit
             user GET    /users/:id(.:format)                        users#show
                  PATCH  /users/:id(.:format)                        users#update
                  PUT    /users/:id(.:format)                        users#update
                  DELETE /users/:id(.:format)                        users#destroy
         sessions POST   /sessions(.:format)                         sessions#create
            login GET    /login(.:format)                            sessions#new
           logout GET    /logout(.:format)                           sessions#destroy
             root GET    /                                           home#index

【问题讨论】:

  • 你能显示你的链接来编辑页面吗?
  • ok
  • 发布你的错误回溯和相关的控制器/表单代码
  • ActionController::UrlGenerationError in Products#edit 没有路由匹配 {:action=>"show", :controller=>"products", :format=>nil, :id=>nil, :user_id =>#} 缺少必需的键:[:id] 指责这一行的错误 {multipart: true} do |f| %>

标签: html css ruby-on-rails


【解决方案1】:

从您的 cmets 中,我发现您缺少为产品传递 ID。

你需要像这样传递@user & @product 对象:

<%= link_to 'Edit', edit_user_product_path(@user, @product) %>

【讨论】:

  • isso nao funcionou, o erro apenas muda para este ActionController::UrlGenerationError in Products#show 没有路由匹配 {:action=>"edit", :controller=>"products", :format=> nil, :id=>nil, :user_id=>#} 缺少必需的键:[:id]
  • 提交表单时是否出现错误?检查更新的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 2014-05-12
相关资源
最近更新 更多