2013-11-25 74 views
0

我正在爲一個學校項目執行此操作,但它無法正常工作。 :( 如何從視圖中獲取選定的[petowner_id]並使其可用於紅寶石控制器? 如何在從視圖進入的控制器中創建@selected_pet = params([petowner_id])功能?目前,它的時候我嘗試設置它呈現的錯誤消息。:( 我已經非常厭倦了沒有工作。從選擇框中獲取數據並將其傳入參數

從寵物控制器

class PetsController < ApplicationController 
    # GET /pets 
    # GET /pets.json 

    def monsters 
    #Finds the current user 
    if current_user 
     @user = current_user 
     @pets_kept = [] #why? 
     #@petowner = Petowner.find(params[:petowner][:id]) 
     #if(params[:commit]) 
     #end 
     @monster = "Eeeep" 
     @mypets=[] 
     @all_my_pets = @user.petowners 
#options value = 2 

#params { selected_petowner[petowner_id]} 
     @selectpet = params{[selected][petowner_id]} 
     @petowner = Petowner.find_by_id(params[:id]) 
     @pet = Pet.find_by_id(params[:pet_id]) 
     #Find the petowners that the user currently has 
     #@mypets = @user.petowners 

     #This is my way of doing things in a C++ fashion, I don't get all ruby things 
     @user.petowners.each do |pet| 
      #@selected_pet = pet.find(params[:selected]) 
     # if pet.hp != 0 
     #  @pets_kept << pet #Dont recall seeing the << before in ruby but for C++ statement used for cout statements 
      #if pet.select 
      # @selected_pet = pet.select 
      #end 
      end 
     #@selected_pet = Petowner.find(params[:petowner][:selected]) 
     #end 
     @selected_pet = 1 #@user.petowners.find(params[:id]) 

     @mypets = current_user.petowners.select{|pet| pet.hp !=0} 
     #raise "I am here" 
     #@selected_pet = @mypets.find(params[:id][:selected]) 
     #@mypets = @pets_kept 
    end 

從視圖代碼中的控制器不會回到控制器變量並設置它:(

<select id="petowner_id" name="selected[petowner_id]"> 
     <%= @all_my_pets.each do |pet| %> 
      <option value="<%= pet.id %>"><%= pet.pet_name %></option> 
     <% end %> 
     </select> 

上一步從寵物/怪物視圖,根本不工作從以前的收藏。 :(

<%= form_for :petowner, :url => petowner_fights_path(@selected_pet, @pet) do |f| %> 

       <p>Select a pet <%#= f.collection_select(:petowner, :petowner_id, @user.petowners, :petowner_id, :pet_name) %></p> 
       <%= render 'monsterinfo' %> 

       <div class="outer"></div> 
       <%= f.submit "Fight Selected Monster" %> 
      <% end %> 
+0

'params {[selected] [petowner_id]}'是無效的語法。我認爲你需要'params [:petowner_id]'。 – lurker

+0

好吧我試過這個語法@selectpet = params [:petowner] [:id]但它不工作。我得到錯誤:PetsController#怪物中的NoMethodError 未定義方法'[]'爲零:NilClass Rails.root:/home/eric/tobenamed.git/Trial Application Trace |框架跟蹤|全面跟蹤 應用程序/控制器/ pets_controller.rb:20:在'怪物 請求 參數: 無 顯示會話轉儲 顯示ENV轉儲 響應 頭: 無 – Eric

+0

嗯,這是不完全是我建議你嘗試的。 ;)但是可以肯定的是,當發生錯誤時,您可以查看params散列的轉儲。 – lurker

回答

0

你可能想params[:petowner][:petowner_id]。絕對不構成選擇與HTML視圖中。

順便說一句,它是真正有益的,看看所有傳遞給控制器​​動作則params的。我傾向於raise params.to_yaml當我需要這樣做。

+0

*嘆了口氣。*我永遠不會從選擇框中得到參數。 :(我試過這個,它給了我一個愚蠢的錯誤信息:( – Eric

+0

你提出了參數嗎?它應該是一個錯誤信息。它會告訴你什麼是包含在參數。 – kddeisz

+0

我提高了參數在25,但我是收到此錯誤:NoMethodError在PetsController#怪物 未定義的方法'[]」的零:NilClass 參數: 無 顯示會話轉儲 顯示ENV轉儲 響應 頭: 無在LN 17當我提出這個消息時,我得到了這個:PetsController#怪物中的RuntimeError ---紅寶石/哈希:的ActiveSupport :: HashWithIndifferentAccess 控制器:寵物 行動:怪物 Rails.root:/首頁/埃裏克/ tobenamed。git /試用版 應用程序跟蹤|框架跟蹤|全面跟蹤 應用程序/控制器/ pets_controller.rb:17:在'怪物 請求 參數: 無 – Eric

相關問題