Ruby-on-rails 在不同的控制器中渲染表单部分(非嵌套)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6189728/
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
Render form partial in a different controller (not nested)
提问by vfilby
I have two models generated with generate scaffolding, one is a LogBook the other is LogEntry. I want to render the form partial for LogEntry on the LogBook show page. When I call render on the partial I get this error:
我有两个使用 generate scaffolding 生成的模型,一个是 LogBook,另一个是 LogEntry。我想在 LogBook 显示页面上为 LogEntry 呈现部分表单。当我在部分上调用 render 时,我收到此错误:
undefined method `model_name' for NilClass:Class
I assume it is because the default _form uses an instance variable which isn't present when called from a separate controller. So I tried converting the LogEntry _form.html.erb to use local variables and passed them in via the render call. After this here is the error:
我认为这是因为默认的 _form 使用了一个实例变量,当从单独的控制器调用时该变量不存在。所以我尝试将 LogEntry _form.html.erb 转换为使用局部变量并通过渲染调用将它们传入。在此之后,这里是错误:
Model LogEntry does not respond to Text
How can I include this partial into the show page form a different controller?
如何将这个部分包含在不同控制器的显示页面中?
Models:
楷模:
class LogBook < ActiveRecord::Base
belongs_to :User
has_many :LogEntries, :dependent => :destroy
end
class LogEntry < ActiveRecord::Base
belongs_to :LogBook, :class_name => "log_book", :foreign_key => "log_book_id"
end
LogEntry _form.html.erb (using local variable):
LogEntry _form.html.erb(使用局部变量):
<%= form_for(log_entry) do |f| %>
<% if log_entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(log_entry.errors.count, "error") %> prohibited this log_entry from being saved:</h2>
<ul>
<% log_entry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Text %><br />
<%= f.text_field :Text %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
LogBook show.html.erb:
日志 show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @log_book.name %>
</p>
<%= render 'log_entries/form', :log_entry => @log_book.LogEntries.new %>
<%= link_to 'Edit', edit_log_book_path(@log_book) %> |
<%= link_to 'Back', log_books_path %>
回答by apneadiving
You can render whatever partial you want as long as you give it's path from the view folder:
你可以渲染任何你想要的部分,只要你从视图文件夹中给出它的路径:
<%= render :partial => '/log_entries/form', :log_entry => @log_book.log_entries.build %>
Your path must begin with a / to let Rails know you're relative to the view folder.
您的路径必须以 / 开头,以让 Rails 知道您是相对于视图文件夹的。
Otherwise it's assumed to be relative to your current folder.
否则,它被假定为相对于您的当前文件夹。
As a sidenote, it's good practive to avoid using instance variables in partial, you did it right then.
作为旁注,避免在部分中使用实例变量是一种很好的做法,您当时就这样做了。
Just seen you have an error in your partial's form:
刚刚看到您的部分表单有错误:
:Text
Should not be a valid column name of your model. Try :text
不应是模型的有效列名。尝试:文本
回答by agmcleod
Try switching the render method as follows:
尝试切换渲染方法如下:
<%= render :partial => 'log_entries/form', :log_entry => @log_book.LogEntries.new %>
Using just render works when passing an instance variable of the object. However, since you're specifying a file, it's best to use the option.
传递对象的实例变量时,仅使用渲染工作。但是,由于您要指定文件,因此最好使用该选项。

