0

When a User create a new account on my app, I want to redirect him to another controller, lets say, a preferences controller.

But I don't know how to do it, the user is created, but it is not been redirect to the new page

Here is the create from user

def create
  @user = User.new(params[:user])

  if params[:user][:newMusician]
    @user.preference = Preference.new
    if @user.save
      sign_in @user
      flash[:success] = "Welcome"
      redirect_to "/preference/edit/"
    else
      render 'new'
    end
  end
end

Here is my routes.rb

resources :users do
  resources :preferences

Just to add, if the relation is has_one, should I still pluralize ? Because each user have just one preference

Another doubt, I just need the edit action in my preferences right ?

Thanks! (Rails 4 Ruby 2)

Eduardo Almeida
  • 410
  • 4
  • 26

1 Answers1

0

In your terminal run rake routes. You should get a route like:

Path (Helper) | Method | URL | Controller#action

edit_user_preference | GET | users/:user_id/preferences/:id/edit | preferences#edit

Prepend the word path and pass the variables user_id and id to the helper method instead of "/preference/edit/":

edit_user_preference_path(@user.id, @preference.id)

You'll also need an update action to update the record.

cortex
  • 5,036
  • 3
  • 31
  • 41
  • Thanks Cortex, the page is redirecting now, but I'm receiving an uninitialized constant PreferencesController – Eduardo Almeida Sep 23 '13 at 22:06
  • On a URL like : localhost/user/14/preference/14 – Eduardo Almeida Sep 24 '13 at 12:19
  • Sure, here is the repo https://github.com/Revokee/NewMusicJungle, but the models are on portuguese... And consider Preference = Musician (Musico, on portuguese). Edit: Here is my Facebook, I don't want to post my email here, but you can chat with me on https://www.facebook.com/eduardo.almeida.754918 – Eduardo Almeida Sep 24 '13 at 20:07