Ruby on Rails Monday, May 5, 2014

Hello Lauree,

I change the way to introduce Ajax on the application, I'm following the "Agile Web development for Rails 4.0".

This is the part of the part of the view related with the issue:
<%= form_for(@comment) do |f| %>
        <% if @comment.errors.any? %>
            <div id="error_explanation">
              <h2><%= pluralize(@comment.errors.count, 'error') %> prohibited this comment from being saved:</h2>

              <ul>
                <% @comment.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
              </ul>                                               r
            </div>
        <% end %>
      <p style= 'padding-top:10px;'>
          <%= f.text_field :text , class:'form-control', required:'', placeholder: 'What is your plan?',style:   'display:table-cell'%>
          <%= f.submit 'Post', class: 'btn', style: 'display:table-cell'   %>
          <%= button_to 'Post', comments_path(comment_id: f),
                      remote: true %>

      </p>

    <% end %>

When 'button_to' is selected in the browser it goes to this action in the comments_controller.rb :

def create
    @comment = Comment.new(comment_params)
    print 'ID of the user that already saved the comment: ' + String(current_user.id)
    @comment.users_id = current_user.id

    respond_to do |format|
      if @comment.save
        format.html { redirect_to user_path(current_user.id) }
        format.js {}
        format.json { render action: 'show', status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

But the line 'format.js' is not launching the file 'create.js.erb' that is in the same folder of the rest of the views of comments... When I check the log it creates the comment and redirect the page to "user_path" but never loads the js file.

Any idea?. I did try with the original app of the book and it works fine, so the problem is in my code.

Thanks & Best regards.




El lunes, 5 de mayo de 2014 06:57:32 UTC+2, Lauree Roberts escribió:

Hello Alfredo,

The problem here is the script tag as you are using it without any attribute as type/language.

Please refer https://developer.mozilla.org/en/docs/Web/HTML/Element/script for more details. The script tag if not given type will be treated as JavaScript tag.

Hence in your case browser is considering that this script tag contains JavaScript code and giving such errors.

For using coffeescript in script tags you can refer http://forgivingworm.wordpress.com/2010/09/27/running-coffeescript-in-browser/


Thanks,
Lauree Roberts
Ruby on Rails Developer
Allerin Technologies




On Saturday, May 3, 2014 11:54:32 PM UTC+5:30, Alfredo Barrero wrote:
Hi all,

I'm starting with Ajax but I have a issue, should be a noobie issue but it makes me crazy hehe. Could anyone tell me what's going on?.

I'm following this guide http://guides.rubyonrails.org/working_with_javascript_in_rails.html, and with the following code my application does not recognize the action.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Hola Mundo con AJAX, version 2</title>

  <script>

      paintIt = (element, backgroundColor, textColor) ->       SHOW ERROS LIKE 'unresolved variable or type element'
              element.style.backgroundColor = backgroundColor
      if textColor?
              element.style.color = textColor

  </script>

</head>
<body><a href="#" onclick="paintIt(this, '#990000')">Paint it red</a></body>
</html>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4fbbcf42-b784-4a77-93c4-cbc59bda2175%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment