2013-03-15 59 views
0

我的form_for没有作为表单传递。我通常可以通过问题排除故障,但是这个我不明白为什么2个参数是从@image传递过来的。这里是我的代码form_for实例变量错误参数数

错误

wrong number of arguments (2 for 1) 

查看

<% form_for @image, :action => "edit" do |f| %> 
    <%= f.text_field :title %> 
    <%= f.submit "Update" %> 
<% end %> 

控制器

class Admin::ImagesController < ApplicationController 
    respond_to :html, :json 
    def index 
     @album = Album.find(params[:album_id]) 
     @images = Image.all 
    end 
    def new 
     @album = Album.find(params[:album_id]) 
     @image = @album.images.new(params[:image_id]) 
    end 
    def create 
     @album = Album.find(params[:album_id]) 
     @image = @album.images.new(params[:image]) 
     if @image.save 
      flash[:notice] = "Successfully added image!" 
      redirect_to [:admin, @album, :images] 
     else 
      render :action => 'new' 
     end 
    end 
    def show 
     @album = Album.find(params[:album_id]) 
     @image = @album.images(params[:id]) 
    end 
    def edit 
     @album = Album.find(params[:album_id]) 
     @image = @album.images(params[:id]) 
    end 
    def update 
     @album = Album.find(params[:album_id]) 
     @image = @album.images(params[:id]) 
     if @image.update_attributes(params[:image]) 
      flash[:notice] = "Successfully updated Image" 
      redirect_to @image 
     else 
      render :action => "edit" 
     end 
    end 
    def destroy 
     @album = Album.find(params[:album_id]) 
     @image = Image.find(params[:id]) 
     @image.destroy 
     redirect_to admin_album_images_path(@album) 
    end 

end 

条路线

Admin::Application.routes.draw do 
    get "albums/index" 

    get "dashboard/index" 

    namespace :admin do 
    root :to => "dashboard#index" 
    resources :dashboard 
    resources :albums do 
     resources :images 
    end 
    get "admin/album" 
    end 
    get "logout" => "sessions#destroy", :as => "logout" 
    get "login" => "sessions#new", :as => "login" 
    get "signup" => "users#new", :as => "signup" 
    # resources :users 
    resources :basic 
    root :to => "basic#index" 

型号

class Image < ActiveRecord::Base 
    attr_accessible :title, :description, :image_name, :image_id, :album_id 
    belongs_to :album 
    accepts_nested_attributes_for :album 
end 
+0

我唯一的红旗将这段代码'@image = @ album.images(PARAMS [:编号])'是不是你应该要做'@image = @ album.images.where(id:params [:id])'? – 2013-03-15 19:24:27

+0

这是你的编辑行为,我假设通过@image ivar传递给你的表单。 – 2013-03-15 19:24:55

+0

它正确地引用了图像......它只是在形式 – 2013-03-15 19:32:28

回答

1

试着改变你的表单代码为以下:

<%= form_for @image, :url => { :action => "edit" } do |f| %> 
    <%= f.text_field :title %> 
    <%= f.submit "Update" %> 
<% end %> 
+0

这消除了错误,但没有呈现表单元素。 – 2013-03-15 19:47:14

+0

@derek_duncan oops。第一行缺少'='。我更新了代码,请尝试再次更改。 – 2013-03-15 19:49:36

1

你缺少find关键字:

def edit 
     @album = Album.find(params[:album_id]) 
     @image = @album.images.find(params[:id]) 
end 

同样的事情会在你的控制器updateshow行动。

有了:

@image = @album.images(params[:id]) 

@images也会包含从相册中的所有图像。

+0

中产生参数错误,这很好。如果我调试@图像,它正确地引用图像。我只是不知道为什么它在edit.html.erb格式中使用'错误参数错误' – 2013-03-15 19:30:05

+0

您可能在该相册中有一个图像?这就是为什么它是KIND OF引用它的原因,但实际上它将返回包含该图像的数组,并且它可以在数组上执行该操作。 – Zippie 2013-03-15 19:37:22

+0

是的,我只在该专辑中有一张图片用于测试。但是,我只是试图选择正在编辑的特定图像... – 2013-03-15 19:39:50