Rails:使用渲染传递参数:动作? [英] Rails: Pass parameters with render :action?

查看:20
本文介绍了Rails:使用渲染传递参数:动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根据调用它的参数显示不同的表单.

I have a form that displays differently depending on the parameter it was called with.

例如

testsite.local/users/new?type=client

因此,如果 typeab,表单将显示不同的字段.

So if type was a or b, the form would display different fields.

我的问题是当表单填写不正确时,因为如果用户无法正确保存,它会使用默认错误消息呈现表单,但也没有我的参数.

My problem is when the form is filled out incorrectly, because if the user couldn't be saved corrently, it renders the form with the default error messages, but also without my parameter.

testsite.local/users/new

如何调用我的 render 操作并将此参数设置为传递给它?以便我仍然可以保留有关为什么无法正确提交表单并使其成为正确表单的内置错误消息?

How can I call my render action and pass whatever this parameter is set to to it? So that I can still keep my built-in error messages about why the form couldn't be sumbitted properly AND have it be the right form?

这是我的 create 操作:

def create
   @user = User.new(params[:user])
   roles = params[:user][:assigned_roles]
   if @user.save
     update_user_roles(@user,roles)
     if current_user.is_admin_or_root?
       flash[:message] = "User "#{@user.username}" created."
       redirect_to users_path
     else
       flash[:message] = "Congrats! You're now registered!"
       redirect_to app_path
     end
   else
     render :action => 'new'
   end
 end

推荐答案

正如另一位用户在回答我的另一个问题时所阐明的那样 相关问题,这是我所追求的:

As elucidated by another user answering my other related question, here's what I was after:

form_for @user, :url => { :action => :create, :type => @type }

... 它通过每个新的渲染操作保留参数 :type(假设我在控制器中正确定义了它).

... which preserves the parameter :type through each new render action (assuming I have it defined correctly in my controller).

这篇关于Rails:使用渲染传递参数:动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆