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

相關問題