【问题标题】:Load js view with jQuery using Rails 4使用 Rails 4 使用 jQuery 加载 js 视图
【发布时间】:2013-10-09 16:54:10
【问题描述】:

我正在学习 Rails,从 Rails 4 开始,但使用的是 Rails 3 书籍。

书籍:Beginning Rails 3 - Cloves Carneiro Jr.和Rida Al Barazi

在某些章节中使用 :format => :js 响应请求

app/views/articles/show.html.erb

<%= render @article %>

<hr>

<h3>Comments</h3>
<div id="comments">
  <%= render @article.comments %>
</div>

<% # render :file => 'comments/new' %>
<%= link_to 'new comment', new_article_comment_path(@article, :format => :js), :remote => true, :id => 'new_comment_link' %>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>

但是这段代码:

<%= link_to 'new comment', new_article_comment_path(@article, :format => :js), :remote => true, :id => 'new_comment_link' %>

没用。

  Rendered comments/new.js.erb (2051.2ms)
Completed 500 Internal Server Error in 2065ms

ActionView::Template::Error (stack level too deep):
  actionpack (4.0.0) lib/action_dispatch/http/mime_type.rb:245


  Rendered c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (17.0ms)

我在app/views/comments/new.js.erb 中创建了一个new.js.erb

$("<%= escape_javascript render(:file => 'comments/new') %>").insertAfter('#comments');
$("#new_comment_link").hide();

但在 Google Chrome 控制台中,请求返回 HTTP 500

耙路线:

https://gist.github.com/6904410

路线

Blog::Application.routes.draw do
  root :to => "articles#index"

  resources :articles do
    resources :comments
  end

  resources :users

  resource :session
  match '/login' => "sessions#new", :as => "login", :via => 'get'
  match '/logout' => "sessions#destroy", :as => 'logout', :via => 'get'

end

model/article.rb

class Article < ActiveRecord::Base

  validates :title, :presence => true
  validates :body, :presence => true

  belongs_to :user
  has_and_belongs_to_many :categories
  has_many :comments

  # Check later
  # accepts_nested_attributes_for :categories

  scope :published, lambda {where("articles.published_at IS NOT NULL")}
  scope :draft, lambda {where("articles.published_at IS NULL")}
  scope :recent, lambda { published.where("articles.published_at > ?", 1.week.ago.to_date) }
  scope :where_title, lambda { |term| where("articles.title LIKE ?", "%#{term}%}") }

  def long_title
    "#{title} - #{published_at}"
  end

  def published?
    published_at.present?
  end

  def owned_by?(owner)
    return false unless owner.is_a? User
    user == owner
  end

end

模型/comment.rb

class Comment < ActiveRecord::Base

  belongs_to :article

  validates :name, :email, :body, :presence => true
  validate :article_should_be_published

  after_create :email_article_author

  def article_should_be_published
    errors.add(:article_id, "is not published yet") if article && !article.published?
  end

  def email_article_author
    logger.info("We will notify #{article.user.email} in Chapter 9")
  end

end

部分解决

我将渲染文件的路径更改为:comments/new.html.erb,并且工作正常。

但是,我不知道这是否是最好/正确的解决方案。例如:在渲染动作中,不要有任何:format 属性。


环境

  • 崇高文本 2
  • Windows 7 专业版 x64
  • Rails 4.0.0
  • Ruby 2.0.0
  • jQuery 1.10

谢谢

【问题讨论】:

  • CommentsController#new 是做什么的?只是渲染 cmets/new.js.erb?您是否按照错误消息的提示检查了日志?
  • 只渲染评论表单。而已。 (只是一本书的例子)
  • @Teoulas 我更新了我的问题。现在可以分析错误了(因为我不懂)

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


【解决方案1】:

问题在于你的

$("<%= escape_javascript render(:file => 'comments/new') %>").insertAfter('#comments');

这里发生的事情是你创建了一个无限循环(yeeeahaa!) 因为

file: 'comments/new'

将渲染 cmets/new.js.erb。但这会称它为自我。

尝试使用以下方法进行测试:

$("<%= escape_javascript render(:partial => 'form') %>").insertAfter('#comments');

它应该可以工作。

【讨论】:

    【解决方案2】:

    解决了!

    我不认为这是更好的解决方案,但有效:

    $("<%= escape_javascript render(:file => 'comments/new', :formats => :html) %>").insertAfter('#comments');
    $("#new_comment").hide().slideDown();
    $("#new_comment_link").hide();
    

    解决方案

    :formats => :html
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 2013-01-23
      相关资源
      最近更新 更多