views:

34

answers:

4

Hello! I try to do following: A user is on his profile page. Now he edits his profile. He klicks on update and the data is saved. Now I want to redirect the user to another kind of profile-edit-page. I did the following in my users_controller.rb:

    def update
        @user = User.find(params[:id])

        respond_to do |format|
          if @user.update_attributes(params[:user])
            flash[:notice] = 'User was successfully updated.'
            if(@user.team_id != nil)
              format.html { redirect_to(@user) }
            else
              format.html { redirect_to choose_team_path }
            end
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
          end
        end
      end


  def choose_team
    @user = User.find(params[:id])
  end

I created a view: /users/choose_team.html.erb

Now I get the following error:

undefined local variable or method `choose_team_path' for #<UsersController:0x1f56650>

So I added choose_team to my routes.rb:

 map.choose_team 'choose-team', :controller => 'users', :action => 'choose_team'

Now, after submitting my first edit form, it redirects me to http://localhost:3000/choose-team and I get following error: Couldn't find User without an ID

What I want: If a user has no team_id, he should be redirected to my choose_team.html.erb for choosing a team, else he should be redirected to his profile/show. How to do this?

EDIT: By writing format.html { redirect_to :action => "choose_team" } instead of format.html { redirect_to choose_team_path } it is working. But how can I get an url like /users/1/choose_team ? At the moment it is just /choose_team .

A: 

You need put this route before your map.resources :users first in first choose with route.rb

shingara
In my routes.rb, on top, I have already added `map.resources :users`
Newbie
A: 

Add following line in route.rb and restatrt your server

map.resources :users, :collection=>{:choose-team=>:get}
Salil
+1  A: 

You have to add your action like this:

map.resources :users, :member=>{:choose-team=>:get}

Because, if you have an action like index, which returns a collection of users than should be put in collection hash. If your action is specific to a user you have to add to the :member hash, now you will get an url like /users/1/choose_team

dombesz
Thank you so much!
Newbie
A: 

the line

format.html { redirect_to choose_team_path }

isn't passing an id to choose_team_path.

Your choose_team action uses the :id param but one isn't being supplied. This is why you get the Couldn't find User without an ID error.

x1a4