简体   繁体   English

form_for instance variable错误的参数个数

[英]form_for instance variable wrong number of arguments

My form_for is not passing as a form. 我的form_for没有作为表单传递。 I can usually troubleshoot through problems, but this one I cannot figure out why 2 arguments are being passed from @image . 我通常可以解决问题,但是这个我无法弄清楚为什么从@image传递了2个参数。 Here is my code 这是我的代码

Error 错误

wrong number of arguments (2 for 1)

View 视图

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

Controller 调节器

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

Routes 路线

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"

Model 模型

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

You are missing the find keyword: 您缺少find关键字:

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

The same thing goes with the update and show action in your controller. 控制器中的updateshow操作也是如此。

With: 附:

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

your @images is going to contain all images from the album. 你的@images将包含相册中的所有图片。

Try changing your form code to the following: 尝试将表单代码更改为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM