Rails: Problem with routes and special Action.

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-06-07T11:32:55Z Indexed on 2010/06/07 11:52 UTC
Read the original article Hit count: 174

Filed under:

Hello! Sorry for this question but I can't find my error! In my Project I have my model called "team". A User can create a "team" or a "contest". The difference between this both is, that contest requires more data than a normal team. So I created the columns in my team table. Well... I also created a new view called 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 %>

In my teams_controller, I created following functions:

  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

Now, I want on my teams/new.html.erb a link to "new_contest.html.erb". So I did:

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

When I go to the /teams/new.html.erb page, I get following error:

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

So I changed in my routes.rb, map.resources :teams to map.resources :teams, :member=>{:new_contest => :get}

Now I get following error: 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?

I don't think adding :member => {...} is the right way doing this. So, can you tell me what to do? I want to have an URL like /teams/new-contest or something.

My next question: what to do (after fixing the first problem), to validate presentence of all fields for new_contest.html.erb? In my normal new.html.erb, a user does not need all the data. But in new_contest.html.erb he does. Is there a way to make a validates_presence_of only for one action (in this case new_contest)?

UPDATE: Now, I removed my :member part from my routes.rb and wrote:

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

Now, clicking on my link, it redirects me to /teams/contest/new - like I wanted - but I get another error called:

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

I think this error is cause of @team at <% form_for @team, :url => { :action => 'create_content_team' } do |f| %>

What to do for solving this error?

© Stack Overflow or respective owner

Related posts about ruby-on-rails