开发者

Adding boolean column to rails database prevents messages from being saved?

开发者 https://www.devze.com 2023-03-27 19:07 出处:网络
def create msg = current_user.msgs.build(params[:msg]) msg.message = msg.message msg.created_at = Time.now # HACK
  def create 
    msg = current_user.msgs.build(params[:msg])
    msg.message = msg.message
    msg.created_at = Time.now # HACK
    if msg.save
    else
      flash[:error] = "Your article must contain some text."
    end
    redirect_to root_path
  end

This is my controller code to save a message, it worked before I tried rails g migration add_anonymous_to_msg anonymous:boo开发者_如何学运维lean and rake db:migrate Now I get the error "your article must contain some text" which means the messages are no longer being saved or the text is not being recognised. I tried removing the column and my schema.rb says that it no longer exists but the error still persists.

Does anyone know what might be the problem? Thanks


How about:

def create 
  @message = current_user.messages.build(params[:message])
  unless @message.save
    flash[:error] = "Your article must contain some text."
  end
  redirect_to root_path
end

Removes the need for the empty if block.


I really think a rewrite of your controller is required, its hard to read and there is a lot of code you don't need. Try something like this:

def create 
  @message = Message.new(params[:message])
  if @message.save
  else
    flash[:error] = "Your article must contain some text."
  end
  redirect_to root_path
end

Secondly, there is most likely an error message in your logs when you submit the form. Any chance you could post that?


you really need to change this line

flash[:error] = "Your article must contain some text."

to

flash[:error] = msg.errors.full_message

from the error message you will know what exactly know what the problem is.

0

精彩评论

暂无评论...
验证码 换一张
取 消