Ruby on rails 例如,form_变量的参数数目错误
我的表格不是作为表格通过的。我通常可以通过问题进行故障排除,但这一次我无法理解为什么从Ruby on rails 例如,form_变量的参数数目错误,ruby-on-rails,ruby-on-rails-3,rails-activerecord,Ruby On Rails,Ruby On Rails 3,Rails Activerecord,我的表格不是作为表格通过的。我通常可以通过问题进行故障排除,但这一次我无法理解为什么从@image传递两个参数。这是我的密码 错误 wrong number of arguments (2 for 1) class Admin::ImagesController < ApplicationController respond_to :html, :json def index @album = Album.find(params[:album_id])
@image
传递两个参数。这是我的密码
错误
wrong number of arguments (2 for 1)
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
查看
<% form_for @image, :action => "edit" do |f| %>
<%= f.text_field :title %>
<%= f.submit "Update" %>
<% end %>
型号
class Image < ActiveRecord::Base
attr_accessible :title, :description, :image_name, :image_id, :album_id
belongs_to :album
accepts_nested_attributes_for :album
end
类映像
您缺少find
关键字:
def edit
@album = Album.find(params[:album_id])
@image = @album.images.find(params[:id])
end
控制器中的更新
和显示
操作也是如此
与:
您的
@images
将包含相册中的所有图像。请尝试将表单代码更改为以下内容:
<%= form_for @image, :url => { :action => "edit" } do |f| %>
<%= f.text_field :title %>
<%= f.submit "Update" %>
<% end %>
{:action=>“edit”}do | f |%>
我唯一的危险信号是这个代码@image=@album.images(params[:id])
你不应该这样做吗@image=@album.images.where(id:params[:id])
?这是你的编辑操作,我假设它是通过@image ivar传递到你的表单上的。它正确地引用了图像的方式。。。当在表单中使用时,它只会产生一个arguments错误。如果Idebug@image
,它将正确引用图像。我只是不知道为什么它在edit.html.erb表单中使用时出现了错误参数错误
。您可能在该相册中有一张图像?这就是为什么它会正确地引用它,但实际上它将返回一个包含该图像的数组,并且它可以在数组上执行该操作。是的,我在该相册中只有一个图像用于测试。但是,我只是尝试选择正在编辑的特定图像…您尝试过此解决方案吗?如果您有:“edit”do | f |%>
而不是@derek|u duncan oops,您的表单将如何显示。第一行中缺少一个=
。我已更新代码,请尝试再次更改。
@image = @album.images(params[:id])
<%= form_for @image, :url => { :action => "edit" } do |f| %>
<%= f.text_field :title %>
<%= f.submit "Update" %>
<% end %>