在不同的控制器中渲染表单部分(非嵌套) [英] Render form partial in a different controller (not nested)

查看:53
本文介绍了在不同的控制器中渲染表单部分(非嵌套)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用 generate scaffolding 生成的模型,一个是 LogBook,另一个是 LogEntry.我想在 LogBook 显示页面上为 LogEntry 呈现部分表单.当我在局部调用 render 时,我收到此错误:

 NilClass:Class 的未定义方法 `model_name'

我认为这是因为默认的 _form 使用了一个实例变量,当从单独的控制器调用时该变量不存在.所以我尝试将 LogEntry _form.html.erb 转换为使用局部变量并通过渲染调用将它们传入.在此之后是错误:

Model LogEntry 不响应 Text

如何将这个部分包含到显示页面中以形成不同的控制器?

型号:

class LogBook :破坏结尾类 LogEntry "log_book", :foreign_key =>log_book_id"结尾

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") %>禁止保存此日志条目:</h2><ul><% log_entry.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="field"><%= f.label :Text %><br/><%= f.text_field :Text %>

<div class="actions"><%= f.submit %>

<%结束%>

LogBook show.html.erb:

<%= notice %>

<p><b>名称:</b><%=@log_book.name %></p><%= render 'log_entries/form', :log_entry =>@log_book.LogEntries.new %><%= link_to '编辑', edit_log_book_path(@log_book) %>|<%= link_to 'Back', log_books_path %>

解决方案

你可以渲染任何你想要的部分,只要你从视图文件夹中给出它的路径:

 <%= render :partial =>'/log_entries/form', :log_entry =>@log_book.log_entries.build %>

你的路径必须以/开头才能让 Rails知道你是相对于视图文件夹的.

否则,它被假定为相对于您当前的文件夹.

作为旁注,避免在部分中使用实例变量是一种很好的做法,您当时就这样做了.

刚刚看到您的部分表单有错误:

 :Text

不应是模型的有效列名.试试 :text

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:

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:

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):

<%= 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:

<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 %>

解决方案

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.

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

这篇关于在不同的控制器中渲染表单部分(非嵌套)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆