【问题标题】:Rails partial error missing template even though there isRails 部分错误缺少模板,即使有
【发布时间】:2016-02-25 00:09:46
【问题描述】:

由于某种原因,显示链接在尝试呈现其部分时抛出错误,即使部分存在(名为 _show.html.erb)

没有更改路由文件。它只是有根声明和resources :notes

而且我并没有删除 Scaffold 生成的原始“show.html.erb”。(问题?)

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
");

我是通过浏览器页面源找到的

这里有什么不正确的地方?

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多