1

我的 Rails 应用程序中有一个联系表。现在它无论如何都只是重定向到主页。如果用户已登录,我想重定向到 user_path,如果未登录,我想重定向到主页。我该怎么做?

*使用设计

联系人控制器

  def create
    @message = Message.new(params[:message])

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      redirect_to(user_path, :notice => "Message was successfully sent.")
    else
      flash.now.alert = "Please fill all fields."
      render :new
    end
  end

end
4

2 回答 2

6

如果他们已登录,您可以重定向到其他地方:

if current_user
    redirect_to(user_path, :notice => "Message was successfully sent.")
else
    redirect_to root_path
end

这是假设您current_xxx的设置为“ user

于 2012-07-12T22:57:37.260 回答
0

使用user_signed_in?助手

def create
  @message = Message.new(params[:message])

  if @message.valid?
    NotificationsMailer.new_message(@message).deliver
    if user_signed_in?
      redirect_to user_path, :notice => "Message was successfully sent."
    else
      redirect_to root_path
    end
  else
    flash.now.alert = "Please fill all fields."
    render :new
  end
end
于 2012-07-12T23:40:14.347 回答