0

当提交表单时,它会传递这些参数。Ruby on Rails将窗体变量传递给控制器​​

Parameters: 

{"utf8"=>"✓", 
"_method"=>"copyfile", 
"authenticity_token"=>"yM2v0dJysGuw7zRIhuhY7xHMywuDRjfBqzpJc0/LCqE=", 
"redocument"=>{"odocument_id"=>"14"}, 
"commit"=>"Update Redocument", 
"method"=>"copyfile", 
"id"=>"66"} 

我想参考在控制器内传递的参数odocument_id

在我的控制器,我有这个

def copyfile 
    @oldfile = Redocument.find(params[:id]) 
    @newfile = Redocument.find(params[:id]).dup 

    @newfile.odocument_id = params[:odocument_id] 
    if @newfile.save! 
     dupfile(@oldfile.redocument.to_s, @newfile.redocument.to_s) 
     flash[:notice] = 'Record was successfully cloned.' 
    else 
     flash[:notice] = 'Record ERROR: Item can\'t be cloned.' 
    end 

    redirect_to(:back) 
    end 

我把它成功地创建新的ID文件夹中的文件。但是,我将我的目录结构分类为odocument_id/redocument_id /。在调用函数dupfile来创建文件夹和副本之前,我无法获取odocument_id进行更新。当我看到@newfile上传到MySQL时,它会创建一个NULL值。

回答

1

您没有正确访问:odocument_id参数。正如您在获得的参数"redocument"=>{"odocument_id"=>"14"}中看到的那样,“odocument_id”位于“redocument”参数内。所以,你需要做的是这样的:

@newfile.odocument_id = params[:redocument][:odocument_id] 

你在做params[:odocument_id]不存在,所以你得到空值。

而且,你可以做

@newfile = @oldfile.dup 

,不需要再次找到该文件。

+0

谢谢Dipil。这工作。我之前有过“确切”的路线,但必须有一个类型-o。有时候,我会重新调用@newfile等冗余调用来让事情顺利进行。 – kobaltz 2011-12-15 04:36:48

相关问题