When i want to create an article. i get this error after clicking on the create article button "No route matches {:action=>"show", :controller=>"articles", :id=>nil} missing required keys: [:id]"
although i make sure there is a user who is logged in before creating the article. Here's my code:
articles_controller.rb
class ArticlesController < ApplicationController
before_action :require_user , except:[:index,:show]
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Your Article was Created"
else
render 'new'
end
redirect_to article_path(@article)
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
flash[:notice] = "Your Article was Updated"
redirect_to article_path(@article)
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
flash[:notice] = "Your Article was deleted"
redirect_to articles_path(@article)
end
private
def article_params
params.require(:article).permit(:title,:description)
end
end
users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "You Signed up successfully"
redirect_to articles_path
else
render 'new'
end
end
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success]= "Your user was updated"
redirect_to articles_path
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:username,:email,:password)
end
end
views/new.html
<h1>New Article</h1>
<%= form_for @article do |f| %>
<p>title<%= f.text_field :title %></p><br>
<p>description<%= f.text_area :description %></p>
<%= f.submit "Create" %>
<%end%>
here's my github repo
Aucun commentaire:
Enregistrer un commentaire