2010-06-07 2 views
0

이 질문에 유감이지만이 오류를 찾을 수 없습니다! 내 프로젝트에는 "팀"이라는 모델이 있습니다. 사용자는 "팀"또는 "컨테스트"를 만들 수 있습니다. 이 두 가지의 차이는 대회가 일반 팀보다 많은 데이터를 필요로한다는 것입니다. 그래서 팀 테이블에 컬럼을 만들었습니다. 내 teams_controller에서 레일 : 경로와 특수 문제가 발생했습니다

<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 %> 

는, 내가 만든 기능 다음 : 음 ... 나는 또한 새로운 뷰라는 create_contest.html.erb 생성 이제

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> 

그래서 I가 내 routes.rb에 map.resources :teams 변경 이제 map.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 => {...}를 추가 생각하지 않는다 올바른 방법이 생을하고있다 에스. 그럼, 나에게 무엇을 할 지 말해 줄 수 있니?/teams/new-contest 같은 URL을 원합니다.

내 다음 질문 : (첫 번째 문제를 해결 한 후) 무엇을 new_contest.html.erb에 대한 모든 필드의 presentence의 유효성을 검사? 내 일반적인 new.html.erb에서 사용자는 모든 데이터가 필요하지 않습니다. 그러나 new_contest.html.erb에서 그는 그렇게합니다. 한 번의 작업 (이 경우 new_contest)에 대해서만 validates_presence_of을 만들 수있는 방법이 있습니까? 이/팀/콘테스트/새 날을 리디렉션,

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

을 이제 내 링크를 클릭 : -

UPDATE : 내 routes.rb에서 멤버 부분과 쓴 : 지금, 나는 내 제거 같은 나는 원했다 -하지만 난라는 또 다른 오류 얻을 : <% form_for @team, :url => { :action => 'create_content_team' } do |f| %>

에서

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

나는이 오류가 @team의 원인이라고 생각을

이 오류를 해결하기 위해 수행해야 할 작업은 무엇입니까?

답변

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_field 또는 create_content 대신 create_contest의 text_fiels처럼 내 .erb 파일에 있었다.

current_user이 (가) 나를 위해 잘 작동합니다.

0

나는 당신의 모델이 어떻게 작동하는지에 대해 잘 모르겠지만, 내 코드에 난 항상 작성했습니다; 대신

@team.user_id = @current_user.id 

ID를 의미

당신에게 오류를 제공하는 컨트롤러에 전달되는되지 않은

@team.user_id = current_user.id 
, 그것을하지 않을까요?

관련 문제