我在两个不同的地方编辑和更新我的个人资料对象,并希望为每个人提供特定的通知。例如,当我在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
答案 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
该代码可以清理很多,但这是它的生成主题。