用户不能在Rails上为空

时间:2015-12-23 22:54:29

标签: ruby-on-rails

您好我正在学习rails并且我犯了一个错误,当我提交文章时错误消息显示但似乎无法知道问题所在。我认为这是一个控制器?

我的代码的Github文件: GitHub Project

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] = "welcome to the alpha blog #{@user.username}"
        redirect_to articles_path
    else
        render 'new'
    end
end


def edit
    @user = User.find(params[:id])
end

def update
    @user = User.find(params[:id])
    if @user.update(user_params)
        flash[:success] = "Your account was updated successfully"
        redirect_to articles_path
    else
        render 'edit'    
    end
end

def show
    @user = User.find(params[:id])
end


private
def user_params
    params.require(:user).permit(:username, :email, :password)        
end
end

articles_controller.rb

class ArticlesController < ApplicationController
before_action :set_article, only: [:edit, :update, :show, :destroy]
# Helppppp!

def index
    @articles = Article.all
end

def new
    @article = Article.new
end

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

end

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

    end
end

def update

    if @article.update(article_params)
        flash[:success] = "Article was successfully updated."
        redirect_to article_path(@article)

    else
        render 'edit'
    end

end

def show

end

def destroy

@article.destroy
flash[:danger] = "Article was successfully deleted"
redirect_to articles_path
end

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

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

2 个答案:

答案 0 :(得分:1)

看看你的方法:

def create
  @article = Article.new(article_params)
  @article.user = User.new 
  ...
end

您正在尝试将空用户分配给文章对象。您应该将user_id作为参数传递(不要忘记在article_params中将其添加为允许的参数),您的代码应为:

@article.user = User.find(article_params[:user_id])

答案 1 :(得分:0)

通过在application_controller.rb中定义current_user方法并添加以下内容来修复它:

**application_controller.rb**
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

def logged_in?
!!current_user
end

def require_user
  if !logged_in?
    flash[:danger] = "You must be logged in to perform that action"
    redirect_to root_path
  end
end
end

<强> articles_controller.rb

class ArticlesController < ApplicationController
before_action :set_article, only: [:edit, :update, :show, :destroy]
before_action :require_user, except: [:index, :show]
before_action :require_same_user, only: [:edit, :update, :destroy]

def index

@articles = Article.paginate(page: params[:page], per_page: 5)

end

def new
    @article = Article.new
end

def edit

end

def create
    @article = Article.new(article_params)
    @article.user = current_user #code that needs fix
    if @article.save
        flash[:success] = "Article was successfully created"
        redirect_to article_path(@article)
    else
        render :new

    end
end

def update

    if @article.update(article_params)
        flash[:success] = "Article was successfully updated."
        redirect_to article_path(@article)

    else
        render 'edit'
    end

end

def show
end

def destroy

@article.destroy
flash[:danger] = "Article was successfully deleted"
redirect_to articles_path
end

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

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

def require_same_user
    if current_user != @article.user
        flash[:danger] = "You can only edit or delete your own article"
        redirect_to root_path

    end

end