0

I am somewhat new to Rails, and I am learning off of a tutorial online. The guy in the videos talked about how you have to have an if statement to decide if the save worked or not.

This is my code:

def create
  @quiz = Quiz.new(quiz_params)
  if @quiz.save
    flash[:notice] = "Your quiz has been created."
    redirect_to action: 'index'
  else
   render('new')
  end
end

The quiz_params was defined as such:

params.require(:quiz).permit(:title, :summary, :visible)

For some reason, it refuses to redirect to the index function, deciding to stay on the same page while the URL changes from localhost:3000/quizzes/new to localhost:3000/quizzes.

The guy who taught the tutorial had this code.

def create
  @subject = Subject.new(subject_params)
  if @subject.save
    flash[:notice] = "Subject created succesfully."
    redirect_to(subjects_path)
  else
    render('new')
  end
end

The subject_params went as such:

 params.require(:subject).permit(:name, :position, :visible)

Other than the wording in the quotes and the name of the model and variables, I am not sure what the difference is. I am being as consistent as I can, so there shouldn't be any difference.

Sorry if the formatting is bad.

fool-dev
  • 6,960
  • 8
  • 32
  • 49

5 Answers5

0

You can run rake routes on your command prompt then they see what is the index_path if got to redirect to the home page then write

redirect_to root_path #=> Or root_url

Or if your controller is QuizzesController then your index path is quizzes_path

redirect_to quizzes_path #=> Use instead of redirect_to action: 'index'

Or if you have articles/index then

redirect_to articles_path

Hope to help

fool-dev
  • 6,960
  • 8
  • 32
  • 49
  • It just renders new and changes the URL, even after running rake routes. It, for some reason, stays on the new template while going to /quizzes. – Jraokmepala Dec 31 '17 at 12:56
  • Have you used "remote: true" on the form? – fool-dev Dec 31 '17 at 12:59
  • What do you mean? – Jraokmepala Dec 31 '17 at 14:51
  • If you use `remote: true` on form for ajax request then I think have to some problem but if you did use that then you need to debug `create` action code I think on `create` method not saving the data if date saved then `redirect_to quizzes_path` work if not saving the data then `render` `new` action – fool-dev Dec 31 '17 at 14:55
  • But where would I put remote: true – Jraokmepala Dec 31 '17 at 15:11
  • You don't need to this now, you need to debug now why not saving data, because if data save then you redirected to index page – fool-dev Dec 31 '17 at 15:13
0

Your validation fails and it renders 'new' view. while action remains to create.

If you will see rake routes this route corresponds to localhost:3000/quizzes to create action.

You can refer this answer for details.

Rahul Sharma
  • 1,172
  • 9
  • 18
0

you can use byebug to debug the request and see if the code is calling redirect_to or not. it could be an ajax form as @fool said. check the form object in the view if it has remote: true attribute, delete it and try again.

Mohamed Elfiky
  • 106
  • 1
  • 7
0

You should tell us Rails and Ruby version. Anyway I am using Rails 5+ and I think the syntax

Render('new') 

Is old or deprecated. What I do usually is this.

def create
    plan = Plan.new plan_params
    plan.delivered = 0
    plan.user_id = params['u_id']
    if plan.save
      flash[:OK] = 'Il nuovo piano e\' stato salvato con successo!'
    else
      flash[:err] = 'Siamo spiacenti ma non siamo riusciti a registrare il tuo piano, ricontrolla i dati inseriti!'
      flash[:errors] = plan.errors.messages
    end
    redirect_to plans_users_path(User.find(params['u_id']))
end

So my advice is try to use named paths instead of strings.

puneet18
  • 4,537
  • 2
  • 18
  • 25
neboduus
  • 350
  • 1
  • 3
  • 13
0

Change below line in controller:

redirect_to action: 'index'

with

redirect_to "/quizzes"

So the code be like:

def create
  @quiz = Quiz.new(quiz_params)
  if @quiz.save
    redirect_to "/quizzes", notice: "Your quiz has been created."
  else
   render 'new'
  end
end
puneet18
  • 4,537
  • 2
  • 18
  • 25