AdilA
@AdilA
Нравится кодить, изучаю go c echo

ActiveRecord::RecordInvalid in CommentsController#create. Как передать юзера?

Пытаюсь приписать юзера но rails ругается ActiveRecord::RecordInvalid in CommentsController#create Возникли ошибки: User не может быть пустым
# Not implemented: check to see whether the user has permission to create a comment on this object
        @comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
        if @comment.save!
          render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
        else
          redirect_to root_path


в контроллере прописано
class CommentsController < ApplicationController
 def create
        @comment_hash = params[:comment]
        @obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
        # Not implemented: check to see whether the user has permission to create a comment on this object
        @comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
        if @comment.save!
          render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
        else
          redirect_to root_path
        end
      end
end


не могу понять в чем проблема?
Использую rails 4.0.0.0
ruby 2.0.0
gem 'acts_as_commentable_with_threading', :github => 'elight/acts_as_commentable_with_threading', :branch => 'master'

по мотивам статьи twocentstudios.com/blog/2012/11/15/simple-ajax-com...

Помогите пожалуйста
  • Вопрос задан
  • 2610 просмотров
Пригласить эксперта
Ответы на вопрос 2
satisFUCKtor
@satisFUCKtor
IT expert
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])

видимо current_user пуст :)
Ответ написан
Able1991
@Able1991
Пишу на рельсах
тут два варианта, либо curent_user возращает объект класса не Актив Рекорд, либо вообще nil в случае если вы запрос аяксом отправляете и не пишете
beforeSend: function(xhr) {
                xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
           }
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы