Ruby on rails Rails根据登录状态重定向

Ruby on rails Rails根据登录状态重定向,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我在rails应用程序中有一个联系人表单。现在,它只是重定向到主页,无论发生什么。如果用户已登录,我想重定向到用户路径,如果未登录,则重定向到主页。。我该怎么做 *使用装置 联络控制器 def create @message = Message.new(params[:message]) if @message.valid? NotificationsMailer.new_message(@message).deliver redirect_to(u

我在rails应用程序中有一个联系人表单。现在,它只是重定向到主页,无论发生什么。如果用户已登录,我想重定向到用户路径,如果未登录,则重定向到主页。。我该怎么做

*使用装置

联络控制器

  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

如果他们已登录,您可以将其重定向到其他位置:

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

这是假设您的
当前\u xxx
设置为“
用户”

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

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

这是假设您的
当前\u xxx
设置为“
用户”

使用
用户登录?
助手

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

使用
user\u signed\u-in?
helper

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

是的,但是如果你不把它钉在上面,我怎么把它贴在我现在的标签上呢;您将其包含在当前的if-else中。只要用coneybeare给你的if-else替换你的重定向_,就行了。对,但我如何将它标记到我当前的if/else(上面列出的)上,如果你不加上它;您将其包含在当前的if-else中。只要用coneybeare给你的if-else替换你的重定向_就行了。