根据Rails 3中路径的重定向,为一个控制器操作提供两个不同的通知

时间:2012-02-24 06:32:59

标签: ruby-on-rails-3

我在两个不同的地方编辑和更新我的个人资料对象,并希望为每个人提供特定的通知。例如,当我在settings_path中编辑/更新配置文件时,我想说“成功更新用户信息”之类的内容。当我在join_path中编辑/更新个人资料时,我想说“创建个人资料”或者根本没有。

这样的事情可能吗?

这是我的Profiles#update行动:

def update
  @profile = user.profile
  if @profile.update_attributes(params[:profile])
    redirect_to profile_path, :notice => 'Updated user information successfully.'
  else
    render :edit
  end
end

1 个答案:

答案 0 :(得分:0)

您可以使用request.referrer检查表单的发布位置,例如

def update
  @profile = user.profile
  if @profile.update_attributes(params[:profile])
    if request.referrer == settings_path
      notice_message = 'Updated user information successfully'
    elsif request.referrer == join_path
      notice_message = 'Profile created'
    else
      notice_message = 'Profile Updated'  # default message
    end

    redirect_to profile_path, :notice => notice_message
  else
    render :edit
  end
end

该代码可以清理很多,但这是它的生成主题。

相关问题