I am teaching myself Ruby and Rails... and until today, have been feeling pretty good about it. :)
However, today I ran into an issue I can't seem to crack. 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.
If the user navigates to: .../article/new, they 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