0

I'm studying Rails, and start with Rails 4, but using a Rails 3 book.

Book: Beginning Rails 3 - Cloves Carneiro Jr.,andRida Al Barazi

In certain chapter Responding to Requests with :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 %>

But this code:

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

Doesn't work.

http://i.imgur.com/6lQ86cS.png

  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)

I create a new.js.erb in app/views/comments/new.js.erb:

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

But in Google Chrome console the request return HTTP 500.

rake routes:

https://gist.github.com/6904410

routes

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

model/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

Partially Solved

I change the path to render file, for: comments/new.html.erb, and works.

But, I don't know if is the best/correct soluction. For example: in render action, don't have any :format attribute.


Environment

  • Sublime Text 2
  • Windows 7 Professional x64
  • Rails 4.0.0
  • Ruby 2.0.0
  • jQuery 1.10

Thanks

3
  • What does CommentsController#new do? Just renders comments/new.js.erb? Did you check the logs, as the error message suggests? Commented Oct 9, 2013 at 18:21
  • Just render the comment form. Nothing more. (Is just a book example) Commented Oct 9, 2013 at 19:16
  • @Teoulas I update my question. Now you can analyse the error (because I not understand) Commented Oct 9, 2013 at 19:22

2 Answers 2

2

Problem is that with your

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

what happend here is that you created an infinite loop (yeeeahaa!) because the

file: 'comments/new'

will render comments/new.js.erb. but this will call it self.

try test it with:

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

and it should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Solved!

I don't if is the better solution, but works:

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

Solution

:formats => :html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.