【问题标题】:form_for instance variable wrong number of argumentsform_for 实例变量错误的参数数量
【发布时间】:2013-03-15 19:19:50
【问题描述】:

我的 form_for 没有作为表单传递。我通常可以解决问题,但是我无法弄清楚为什么从@image 传递了两个参数。这是我的代码

错误

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

【问题讨论】:

  • 我唯一的危险信号就是这个代码@image = @album.images(params[:id]) 你不应该这样做@image = @album.images.where(id: params[:id])吗?
  • 这是我假设通过@image ivar 传递给您的表单的编辑操作。
  • 它正确地引用了图像……它只是在表单中使用时产生参数错误

标签: ruby-on-rails ruby-on-rails-3 rails-activerecord


【解决方案1】:

尝试将您的表单代码更改为以下内容:

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

【讨论】:

  • @derek_duncan 哎呀。第一行缺少=。我已更新代码,请尝试再次更改。
【解决方案2】:

您缺少find 关键字:

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

控制器中的updateshow 操作也是如此。

与:

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

您的@images 将包含相册中的所有图片。

【讨论】:

  • 这很好用。如果我debug @image,它会正确引用图像。我只是不知道为什么它在edit.html.erb表单中使用时有wrong arguments error
  • 您的相册中可能只有一张图片?这就是为什么它是正确引用它的原因,但实际上它会返回一个包含该图像的数组,并且它可以对数组执行该操作。
  • 是的,我在该相册中只有一张图片用于测试。但是,我只是想选择正在编辑的特定图像...
  • 你试过这个解决方案了吗?
  • 如果您的表单有:&lt;% form_for @image, :action =&gt; "edit" do |f| %&gt; 而不是 &lt;%=.. api.rubyonrails.org/classes/ActionView/Helpers/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 2010-10-31
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多