Rails部分错误丢失模板,即使有

时间:2015-11-22 23:47:18

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

由于某种原因,即使部分存在(名为_show.html.erb)

,show link也会在尝试渲染其部分时抛出错误

对路线文件没有任何更改。它只有根声明和resources :notes

我还没有删除原来的" show.html.erb"由Scaffold制作。(事项?)

index.html.erb

<% @notes.each do |note| %>
  <% note.title %>
  <%= link_to 'Show', note, remote: true, class: "note-show" %> 
<% end %>

<div id="note-show">

</div>

_show.html.erb

<% note.title %>
<% note.body %>

show.js.erb

$('#note-show').html("<%= render :partial => 'show' %>");

备注控制器

class NotesController < ApplicationController
before_action :set_note, only: [:show, :edit, :update, :destroy]
def index
    @notes = Note.all.order('created_at DESC')
end

def show
    respond_to do |format|               
      format.js
      format.html
    end  
end

private
    # Use callbacks to share common setup or constraints between actions.
    def set_note
      @note = Note.find(params[:id])
    end

错误

ActionView::MissingTemplate in Notes#show

Showing /home/arjun/rails/notes/app/views/notes/show.html.erb where line # raised:
Missing partial notes/_show, application/_show with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/home/arjun/rails/notes/app/views"
  * "/home/arjun/.rvm/gems/ruby-1.9.1-p431/gems/twitter-bootstrap-rails-3.2.0/app/views"

奇怪的是,在某些链接上只显示

$('#note-show').html("hello
");

我是通过使用浏览器页面源

找到的

这里有什么不对?

1 个答案:

答案 0 :(得分:0)

在show.js.erb中你必须转义javascript,(escape_javascript或j)

$('#note-show').html("<%= j render 'show' %>");