← Back to Upcase

How do I create a url path?


(Charlieanna) #1

A user has joined a challenge. Now he has to create a team with people who joins the team and all of them automatically get that challenge.

So currently I am on the challenges page and I need to tell the user to create a team. How do I make sure the team that is created with respective to the challenge gets that challenge and is propogated to all the team members?

I am having tough time designing the urls and the link_tos. :frowning:

I am on the challenge show page and this is the link_to I have come up.

<%= link_to "Invite friends by creating a team.", "/challenges/#{@challenge.id}/teams/new" %>

(Luís Ferreira) #2

Not sure I understand you question, but does this help?

resources :challenges do
  resources :teams, only: [:new, :create]
end

(Charlieanna) #3

But what If I want to create a team and then create a challenge for that team?


(Luís Ferreira) #4

Never had that problem, but I guess you can do:

resources :challenges do
  resources :teams, only: [:new, :create]
end

resources :teams do
  resources :challenges, only: [:new, :create]
end

(Charlieanna) #5

But is this solution fine to have? I was using your solution and wondered if there is any better solution than that.


(Luís Ferreira) #6

You can also do this:

resources :challenges
resources :teams

and pass the challenge_id as a url variable:

new_team_path(challenge_id: 1)

this will generate the following url:

/teams/new?challenge_id=1

Not sure which one is the best, or if there is a better way.


(Charlieanna) #7

Ya. I think this one makes more sense. I hate seeing teams/1/challenges/1 or challenges/1/teams/1 in my urls.

Will ask my boss tomorrow what he likes. :wink: