[英]Rails routes, problem with custom :action not being recognized, always 'show'
[英]Rails: Problem with routes and special Action
很抱歉這個問題,但是我找不到我的錯誤! 在我的項目中,我有一個名為“團隊”的模型。 用戶可以創建“團隊”或“競賽”。 兩者之間的區別在於,比賽需要比普通團隊更多的數據。 因此,我在團隊表中創建了列。 好吧...我還創建了一個名為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
現在,我想在我的team / new.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>
所以我將自己的route.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?
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所有字段的存在? 在我正常的new.html.erb中,用戶不需要所有數據。 但是他在new_contest.html.erb中做到了。 有沒有一種方法可以只對一個動作(在本例中為new_contest)進行validates_presence_of
?
更新:現在,我從routes.rb中刪除了:member部分,並寫道:
map.new_contest '/teams/contest/new', :controller => 'teams', :action => 'new_contest'
現在,單擊我的鏈接,它將我重定向到/ teams / contest / new-就像我想要的-但出現另一個錯誤:
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| %>
<% form_for @team, :url => { :action => 'create_content_team' } do |f| %>
解決該錯誤該怎么辦?
我不確定您的模型如何工作,但是在我的代碼中,我總是寫的。
@team.user_id = @current_user.id
代替
@team.user_id = current_user.id
那意味着沒有將ID傳遞給控制器,從而給您錯誤,不是嗎?
好的,我發現了我的錯誤。 作為記錄:首先,我忘了在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
我的.erb文件中也有多種錯別字,例如text_fiels而不是text_field或create_content而不是create_contest。
current_user對我來說很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.