根据 Rails 中的另一个操作显示更新操作的不同通知消息

Show different notice message for the update action based on another action in Rails

我正在让一个用户在船上显示一个表单,其中包含从 Twitter 提交和获取的数据,我使用相同的表单进行 update 操作,但在不同的路径中,如果用户提交了他的表单将被定向到他的个人资料页面(对他的个人资料进行基本更新)。我只想要此步骤的不同通知消息,因为它使用的是 update 操作一,因此用户可以知道配置文件已创建且未更新。

users/finish_signup.html.haml

= simple_form_for(@user) do |f|
  = f.input_field :name
  = f.submit t('go')

路线

get ':id', :to => "users#show"
get ':id/finish', :to => "users#finish_signup", :as => 'finish_signup'

完成完成注册方法

def finish_signup
    # ...
    # for example
    # redirect_to user_show_path_helper(@user), :notice => t('sucess_profile_created')
end

更新方法

def update
    @user = User.friendly.find(params[:id])
    @page_has_darker_background = true

    if @user.update(user_params)
      redirect_to user_show_path_helper(@user), :notice => t('sucess_profile_update')
    else
      render 'edit'
    end

end

谢谢

完成 @user.update(user_params) 后,您检查您的 user_params 并相应地设置通知消息。 例如。 if params[:user][:first_name] then notice: first name updated OR params[:user][:last_name] 然后 notice: 姓氏更新,然后 redirect_to user_show_path_helper(@user)

正如@gaurav 所建议的,我添加了一个隐藏字段并基于它进行了验证。

表格

= simple_form_for(@user) do |f|
  = f.input_field :finish_signup, as: :hidden, value: 'finish_signup'
  = f.input_field :name
  = f.submit t('go')

更新

def update
    @user = User.friendly.find(params[:id])
    @page_has_darker_background = true

    if @user.update(user_params)
      redirect_to user_show_path_helper(@user), :notice => params['user']['finish_signup'] ? t('created') : t('update')
    else
      render 'edit'
    end
  end