Ruby-Rails redirect_to Not Working Properly?

rails
ruby

#1

Hello

I recently moved over to Cloud 9 from Nitrous and have been really like it… but I’m a bit confused because things that worked on Nitrous are not working here… I don’t know if it’s a setting or if I need to do something different…

I am teaching myself… and until today, have been feeling pretty good about it… I ran into an issue I can’t seem to crack and am hoping for a little guidance. I am practicing by creating a blog. I have the routes and views set up - and the code will write to the database - but the redirect_to doesn’t seem to be working properly.

By navigating to: …/article/new, the user can enter a new article. Upon successful submission, the app should redirect to the URL …/article/id, display the article on show.html.erb and flash a message that the article saved successfully.

For some reason, I cannot get the URL redirect portion to work. The article saves, the show.html.erb displays with the proper message… but the url stays …/article/new. If the redirect doesn’t work, the rest of the application won’t work because other actions require different URLs (…/article/id/edit, for example).

I have been using

<%= @article.inspect %> 

on the show page and it looks like everything has saved correctly (which I suspected because it calls the correct view).
Any thoughts would be very, very welcome. I’m really excited to be learning Ruby and Rails and look forward to getting through this.

articles_controller.rb:

class ArticlesController < ApplicationController
  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
      flash[:notice] = "Article was successfully created"
      redirect_to article_path(@article)
    else
      render 'new'
    end
  end

  def show
    @article = Article.find(params[:id])
  end

  private
    def article_params
    params.require(:article).permit(:title, :description)
  end
end

rake routes:

Prefix Verb   URI Pattern                  Controller#Action
        root GET    /                            pages#home
       about GET    /about(.:format)             pages#about
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy

#2

Do you have the source code somewhere on github?


#3

Hi - I just pushed the code that works from Nitrous to github.

Thanks so much for any input you have…

I dont know if it matters - but when I imported the code from Nitrous (working) into Cloud9 (not working), Cloud9 wanted me to update my gemfile, specifically for railties.

I ran rake bundle and all of my gems are the same with the exception of bundler (1.11.2 on cloud9 vs 1.10.5 on nitrious).


#4

I cloned your project and after creating a new article I’m redirected to the show page of the article, maybe it’s an issue on your end.

By the way, I suggest you to add a before_action filter to look for the article id when editing, showing and deleting, so you don’t have to find it on every action.


#5

Hi Oxyrus - thank you so much much for your help… when you are redirected to the show page, does the url change?

The issue I am having is that I am redirected to the show page - but the URL does not change… it stays …/articles/notes - even on the show page. It should (and does on nitrous) display the show page but then changes the URL to …/articles/id.

And thanks for the tip to add the before_action to look for the article ID!


#6

When I create the article I’m redirected to /articles/:id successfully.


#7

This is very odd. Do you have any idea what could be causing this issue? I have been teaching myself and am lost…

This is a test I did from cloud9 just before posting the image.

Are there settings in Cloud9 I need to update or anything to ensure I am running the latest version of Ruby and Rails?

Thank you again!


#8

and for comparison, this is a test I ran from Nitrous just before posting the image.


#9

Have you tried running the application on a browser window instead of C9 window?


#10

Wow - that worked! Thank you so much! Just so I know, what is the difference? I have been working on this for three days. :confused: (I wanted to exhaust everything I could think of before asking for help… you know - learning and all).


#11

Well, sometimes C9 browser is kinda buggy, that’s why I always open a different window to see my application running.

Good luck with your journey on RoR :slight_smile: