ImageConject中的ActionController :: UrlGenerationError

时间:2014-03-15 02:26:06

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

新手在这里坚持嵌套资源。我有一个image.rb模型,它将图像存储在cloudinary CDN上。在完成所有工作之后,我决定我需要对图像进行分类,所以我创建了一个category.rb模型,它现在只有一个名字,无论如何。

在添加category模型和控制器/视图后,我一直在慢慢处理错误,所有这些都没问题,直到这一次。我很难过哈哈。现在谷歌搜索了几个小时,并决定我在墙上......所以,我自然而然地来到这里,希望有人能够启发我。提前致谢! < 3 SO

这是错误(它当前正在"edit"链接上,但我认为它会在我编辑工作后将它抛到"delete"上,所以我一直在改变它们那里):

No route matches {:category_id=>nil, :id=>#<Image id: 16, title: "mario", description: "i belong to category 2!", upload_date: nil, created_at: "2014-03-15 01:13:54", updated_at: "2014-03-15 01:13:54", category_id: 2>} missing required keys: [:category_id]

<td><%= link_to "Show", [@category, image] %></td>
<% if admin? %>
        <td><%= link_to "Edit", edit_category_image_path(@category, image) %></td>
        <td><%= link_to "Delete", [@category, image], confirm: "Are you sure you want to delete \"#{image.title}\"?", method: :delete %></td>
<% end %>
    </tr>

这是我的Image模型:

class Image < ActiveRecord::Base
  has_attachment :image_file, accept: [:jpg, :png, :gif]
  validates :title, presence: true

  belongs_to :category
end

以下是Category型号:

class Category < ActiveRecord::Base
  has_many :images
end

以下是每个控制器:

class ImagesController < ApplicationController
  before_filter :authenticate_admin, except: [:index, :show]
  def index
    @images = Image.all
  end

  def show
    @image = Image.find(params[:category_id])
  end

  def new
    @category = Category.find(params[:category_id])
    @image = @category.images.build(image_params)
  end

  def create
    @category = Category.find(params[:category_id])
    @image = @category.images.build(image_params)

    if @image.save
      render "show"
    else
      render "new"
    end
  end

  def destroy
    @image = Image.find(params[:id])
    @image.destroy
    redirect_to images_path
  end

  def edit
    @category = Category.find(params[:category_id])
    @image = @category.images.find(params[:id])
  end

  def update
    @category = Category.find(params[:category_id])
    @image = @category.images.find(params[:id])

    if @image.update(image_params)
      render "show"
    else
      render "edit"
    end
  end

  private

  def image_params
    params.require(:image).permit(:title, :description, :image_file, :category_id)
  end
end

Category控制器

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end

  def show
    redirect_to category_images_path(params[:id])
  end

  def new
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)

    if @category.save
      redirect_to @category
    else
      render "new"
    end
  end

  def destroy
    @category = Category.find(params[:id])
    @category.destroy
    redirect_to categories_path
  end

  def edit
    @category = Category.find(params[:id])
  end

  def update
    @category = Category.find(params[:id])

    if @category.update(category_params)
      redirect_to @category
    else
      render "edit"
    end
  end

  private
  def category_params
    params.require(:category).permit(:title)
  end
end

这是路线文件:

Portfolio::Application.routes.draw do
  devise_for :users
  resources :blogs

  resources :categories do
    resources :images
  end


  root "index#index"

  #attachinary
  mount Attachinary::Engine => "/attachinary"
end

通过Google搜索,我一直遇到与form partials有关的人遇到的问题,所以这里是我_form.html.erb的{​​{1}},以防万一需要:

Image

1 个答案:

答案 0 :(得分:0)

ImagesController

class ImagesController < ApplicationController
  before_action :load_category
  before_action :set_image, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_admin, except: [:index, :show]
  def index
    @images = @category.images.all
  end

  def show
  end

  def new
    @image = @category.images.new
  end

  def create
    @image = @category.images.new(image_params)

    if @image.save
      render "show"
    else
      render "new"
    end
  end

  def destroy
    @image.destroy
    redirect_to images_path
  end

  def edit
  end

  def update
    if @image.update(image_params)
      render "show"
    else
      render "edit"
    end
  end

  private

  def load_category
    @category = Category.find(params[:category_id])
  end

  def set_image
    @image = @category.images.find(params[:id])
  end

  def image_params
    params.require(:image).permit(:title, :description, :image_file, :category_id)
  end
end

尝试使用before_action回调来设置必要的实例变量。在这里,我通过私有方法加载类别和图像。这节省了很多时间。您甚至可以在生成脚手架时看到这一点。

before_filter已被别名为before_action,以便更清楚其功能。

根路径通常在第二行路线上定义,因此它是明确的。

您的命名路径似乎不是问题。它主要是你的图像控制器

我建议您使用脚手架生成一个新的rails应用程序,只是为了查看它的控制器。

rails g scaffolding images