简体   繁体   English

Rails:使用render:action传递参数?

[英]Rails: Pass parameters with render :action?

I have a form that displays differently depending on the parameter it was called with. 我有一个表单,根据调用它的参数显示不同。

Ex. 防爆。

testsite.local/users/new?type=client

So if type was a or b , the form would display different fields. 因此,如果typeab ,则表单将显示不同的字段。

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

How can I call my render action and pass whatever this parameter is set to to it? 如何调用我的render操作并将此参数设置的任何内容传递给它? 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? 所以我仍然可以保留我内置的错误消息,说明为什么表单无法正确汇总并使其成为正确的表单?

Here's my create action: 这是我的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 }

... which preserves the parameter :type through each new render action (assuming I have it defined correctly in my controller). ...保留参数:type通过每个新的渲染动作:type (假设我在控制器中正确定义了它)。

There are two ways to do it. 有两种方法可以做到这一点。

Either add a hidden field or an extra parameter to the form_for 向form_for添加隐藏字段或额外参数

Adding hidden field in your form containing named type and set it equal to params[:type] this will preserve the type on the render if validation fails. 在包含命名类型的表单中添加隐藏字段并将其设置为等于params [:type],如果验证失败,这将保留渲染上的类型。

View code: 查看代码:

<% form_for @user do |f| %>
...
<%= hidden_field_tag :type, params[:type] %>
<%end%>

Adding an extra paramater to the form: 在表单中添加额外的参数:

<% form_for @user, create_user_path(@user, :type => params[:type]) %>
...
<%end%>

Either one will do what you want it to. 任何一个人都会做你想要的。

Extract for Rails Guides Rails指南提取

Using render with :action is a frequent source of confusion for Rails newcomers. 使用render with:action是Rails新手的常见混淆源。 The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller 指定的操作用于确定要呈现的视图,但Rails不会在控制器中运行该操作的任何代码

I believe your new?type=client is used to setup some kind of variable in your new action? 我相信你的new?type=client用于在你的新动作中设置某种变量? And the variable is later used in your new.html.erb view? 变量稍后在new.html.erb视图中使用?

Then you need to change a little bit your create action like this: 然后你需要改变你的创建动作,如下所示:

  else
     # add code here to setup any variable for type = client
     # and which will be used in your view
     render :action => 'new'
  end
 end

You could always force it by: 你可以随时强迫它:

params[:type] ||= "client"

in your new action in your controller. 在您的控制器中的新操作中。 If type is not set then it will default to client. 如果未设置type,则默认为client。 The URL will not show this however. 但是,URL不会显示此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM