我有两个生成脚本的模型,一个是LogBook,另一个是LogEntry.我想在LogBook展示页面上呈现LogEntry的部分格式.当我调用render部分我得到这个错误:
undefined method `model_name' for NilClass:Class
我认为这是因为默认_form使用一个实例变量,当从单独的控制器调用时,该实例变量不存在.所以我尝试将LogEntry _form.html.erb转换为使用局部变量,并通过render调用传递它们.之后这里是错误:
Model LogEntry does not respond to Text
楷模:
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(使用局部变量):
<%= 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 %>