简体   繁体   English

从Ruby on Rails 3中的另一个控制器渲染表单

[英]Rendering a form from another controller in Ruby on Rails 3

Ok, the problem is trying to render a form from a controller to create a new entry.. For that im calling the default form that comes with the scaffold creation I'm trying to make it like this: 好吧,问题是尝试从控制器呈现一个表单来创建一个新条目..为此我调用脚手架创建附带的默认表单我试图使它像这样:

<%= render :partial => 'contactos/form'  %> 

And im getting the following error 我得到以下错误

undefined method 'model_name' for NilClass:Class

Is there any way of just rendering from the view itself? 有没有办法只从视图本身渲染?

If there is not... Which parameters should I add to the controller? 如果没有...我应该将哪些参数添加到控制器?

Right now I just have following code: 现在我只有以下代码:

Class DisplayController < ApplicationController
     def index
        @contactos = Contacto.all
     end
end

*This is the view controller, not the one with the create update and edit functions from my scaffold *这是视图控制器,而不是我的脚手架中具有创建更新和编辑功能的视图控制器

Ok, I've done a very large research but no answer can fix my problem. 好的,我做了一个非常大的研究,但没有答案可以解决我的问题。 (This is the first time I ask something, sorry in advance for any mistake I could make) (这是我第一次提出问题,提前抱歉我可以犯的任何错误)

The problem is that the variable your are using in the form for your contact doesn't exist. 问题是您在联系表单中使用的变量不存在。 The only variable you created in the index action is an array of all the contacts, but the form needs a single instance of a single contact. 您在索引操作中创建的唯一变量是所有联系人的数组,但表单需要单个联系人的单个实例。

Because you are making a new contact, you have to do something like this in the action index: 因为您正在建立新联系人,所以您必须在动作索引中执行以下操作:

@contact = Contact.new

Generally this happens when we use an instance variable in the view but it is nil. 通常,当我们在视图中使用实例变量但它为零时会发生这种情况。

For eg: in new user creation form we use, 例如:在我们使用的新用户创建表单中,

form_for @user do |f|
....
end

and @user is not initialized in the controller action, from where we render the form, this can happen. 并且@user未在控制器操作中初始化,从我们呈现表单的位置,这可能发生。

you need to make sure that the param used in the form partial is defined before you render the partial. 您需要确保在呈现部分之前定义了部分表单中使用的参数。

Also you don't need the partial, you can just put: 你也不需要局部,你可以放:

<%= render 'contactos/form'  %> 

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

相关问题 来自另一个控制器的Rails3渲染动作 - Rails3 rendering action from another controller 在轨道上局部渲染另一个控制器的视图红宝石 - Partial Rendering of another controller's view ruby on rails 在Ruby on Rails中,我可以使用新的控制器来显示从另一个控制器上的表单获取的列信息吗? - Within Ruby on Rails, can I use a new controller to display column information obtained from a form on another controller? Ruby on Rails:如何通过表单将实例变量从控制器传递到另一个控制器 - Ruby on Rails: how to pass an instance variable from a controller to another controller via a form Ruby on Rails:控制器中的自定义渲染 - Ruby on Rails: custom rendering in controller Ruby on Rails:从控制器中的表单访问值 - Ruby on Rails: Access values from form in controller Ruby on Rails-通过另一个控制器传递ID - Ruby on Rails - Pass ID from another controller Ruby on Rails-从另一个控制器渲染部分 - Ruby on Rails - Render partial from another controller Ruby on Rails 4-将答案从一个控制器传递到另一个控制器 - Ruby on Rails 4 - Pass answer from one controller to another controller 在Ruby on rails上将数据从一个控制器提交到另一个控制器模型 - Submit data from one controller to another controller model in Ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM