2010-06-07 30 views
0

對不起,這個問題,但我找不到我的錯誤! 在我的項目中,我有我的模型叫做「團隊」。 用戶可以創建一個「團隊」或「比賽」。這兩者之間的區別在於,比賽需要比正常隊伍更多的數據。 所以我創建了我的團隊表中的列。 嗯...我還創建了一個新的觀點叫做create_contest.html.erb:導軌:問題與路線和特殊行動

<h1>New team content</h1> 

<% form_for @team, :url => { :action => 'create_content' } do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </p> 
    <p> 
    <%= f.label :description %><br /> 
    <%= f.text_area :description %> 
    </p> 
    <p> 
    <%= f.label :url %><br /> 
    <%= f.text_fiels :url %> 
    </p> 
    <p> 
    <%= f.label :contact_name %><br /> 
    <%= f.text_fiels :contact_name %> 
    </p> 
    <p> 
    <%= f.submit 'Create' %> 
    </p> 
<% end %> 

在我teams_controller,我創建了以下功能:

def new_contest 
    end 

    def create_contest 
    if @can_create 

     @team = Team.new(params[:team]) 
     @team.user_id = current_user.id 
     respond_to do |format| 
     if @team.save 
      format.html { redirect_to(@team, :notice => 'Contest was successfully created.') } 
      format.xml { render :xml => @team, :status => :created, :location => @team } 
     else 
      format.html { render :action => "new" } 
      format.xml { render :xml => @team.errors, :status => :unprocessable_entity } 
     end 
     end 
    else 
     redirect_back_or_default('/') 
    end 
    end 

現在,我想在我的球隊/新.html.erb指向「new_contest.html.erb」的鏈接。 所以我做:

<%= link_to 'click here for new contest!', new_contest_team_path %> 

當我去/teams/new.html.erb頁面,我得到以下錯誤:

undefined local variable or method `new_contest_team_path' for #<ActionView::Base:0x16fc4f7> 

所以我在routes.rb中發生變化,map.resources :teamsmap.resources :teams, :member=>{:new_contest => :get}

現在我得到以下錯誤:new_contest_team_url failed to generate from {:controller=>"teams", :action=>"new_contest"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["teams", :id, "new_contest"] - are they all satisfied?

我不認爲加入:member => {...}是正確的方式做THI秒。那麼,你能告訴我該怎麼辦?我想要一個像/ teams/new-contest之類的網址。

我的下一個問題:如何解決第一個問題,以確認new_contest.html.erb所有字段的前提條件?在我的正常new.html.erb中,用戶不需要所有的數據。但是他在new_contest.html.erb中。有沒有辦法讓一個validates_presence_of只有一個動作(在這種情況下new_contest)?

UPDATE:從我的routes.rb體部和中寫道: 現在,我刪除了我的

map.new_contest '/teams/contest/new', :controller => 'teams', :action => 'new_contest' 

現在,點擊我的鏈接,它重定向我/團隊/較量/新 - 像我想要的 - 但我得到叫另一個錯誤:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id 

我認爲這是錯誤的@team原因在<% form_for @team, :url => { :action => 'create_content_team' } do |f| %>

如何解決這個錯誤?

回答

0

好吧,我發現我的錯誤。 備案: 首先,我忘了寫我的def new_contest裏面的代碼。那就是:

def new_contest 
    if @can_create 
     @team = Team.new 
     respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @team } 
     end 
    else 
     redirect_back_or_default('/') 
    end 
    end 

有幾個錯別字,也像text_fiels代替text_field或create_content代替create_contest我.erb文件。

current_user對我來說工作正常。

0

我不確定你的模型是如何工作的,但是在我一直寫的代碼中;

@team.user_id = @current_user.id 

代替

@team.user_id = current_user.id 

這將意味着該ID沒有被傳遞給控制器​​給你的錯誤,不是嗎?