Rails:通过render:action传递参数吗? [英] Rails: Pass parameters with render :action?

查看:209
本文介绍了Rails:通过render:action传递参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,该表单根据调用它的参数而有所不同.

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:通过render:action传递参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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