Извините за этот вопрос, но я не могу найти свою ошибку!
В моем проекте моя модель называется «команда».
Пользователь может создать «команду» или «конкурс». Разница между ними заключается в том, что для соревнования требуется больше данных, чем для обычной команды.
Поэтому я создал столбцы в моей командной таблице.
Хорошо ... Я также создал новый вид с именем 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 %>
В моем team_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
Теперь я хочу в своих командах / 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?
Я не думаю, что добавление :member => {...}
является правильным способом сделать это. Итак, вы можете сказать мне, что делать? Я хочу, чтобы URL был похож на / команды / новый-конкурс или что-то еще.
Мой следующий вопрос: что делать (после исправления первой проблемы), чтобы проверить наличие всех полей для new_contest.html.erb? В моем обычном new.html.erb пользователю не нужны все данные. Но в new_contest.html.erb он делает. Есть ли способ сделать validates_presence_of
только для одного действия (в данном случае new_contest)?
UPDATE:
Теперь я удалил свою часть: member из моего rout.rb и написал:
map.new_contest '/teams/contest/new', :controller => 'teams', :action => 'new_contest'
Теперь, нажимая на мою ссылку, он перенаправляет меня в / команды /test / 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| %>
Что делать для решения этой ошибки?