Ruby-on-rails Rails:使用渲染传递参数:动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1549055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:51:10  来源:igfitidea点击:

Rails: Pass parameters with render :action?

ruby-on-railsparametersrender

提问by neezer

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

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

Ex.

前任。

testsite.local/users/new?type=client

So if typewas aor 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 renderaction 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?

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

Here's my createaction:

这是我的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

采纳答案by neezer

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 :typethrough each new render action (assuming I have it defined correctly in my controller).

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

回答by EmFi

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.

任何一个都会做你想要的。

回答by Aurélien Bottazini

Extract for Rails Guides

Rails 指南摘录

Using render with :action is a frequent source of confusion for Rails newcomers. 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

使用带有 :action 的 render 是 Rails 新手经常感到困惑的原因。指定的动作用于确定要呈现哪个视图,但 Rails 不会在控制器中运行该动作的任何代码

I believe your new?type=clientis used to setup some kind of variable in your new action? And the variable is later used in your new.html.erb view?

我相信你new?type=client是用来在你的新动作中设置某种变量的吗?该变量稍后会在您的 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

回答by Ryan Bigg

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. The URL will not show this however.

在您的控制器中的新操作中。如果未设置类型,则默认为客户端。但是,该 URL 不会显示此内容。