I am trying to create my first ever program Using Ruby programming
language on Ruby on Rails and it is a blog! I am following a tutorial
and have created a comments box which does NOT print the comment on the
page so far when written in the comments box, that's the problem. I will
explain step by step I have:

1. Called out on routes.rb:

resources :posts do
resources :comments
end

2. Called out using Cmder:
rails g controller comments create destroy


3. This is my comments_controller.rb class:

class CommentsController < ApplicationController
   def create
     @post = Post.find(params[:post_id])
     @comment = @post.comments.build(params[:comment]) #around here is
the error
     @comment.save

     redirect_to @post
   end

   def destroy
   end
end



can anybody please help?

--
Posted via http://www.ruby-forum.com/.

--
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/335bf9a03da77a47707ee83e516d92d4%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Richard Wilson at Sep 1, 2014 at 5:23 am
    The routes don't matter here.

    Firstly, it should be @comment = @post.comments.create instead of build, no
    point in building something and saving it afterwards.

    Secondly, the comment won't be created because the parameters for the
    comment are not being sanitized. If rails allowed you to do what you are
    wanting to do here it would be a terrible security issue.

    See http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
    On Saturday, August 30, 2014 5:40:35 PM UTC-7, Ruby-Forum.com User wrote:

    I am trying to create my first ever program Using Ruby programming
    language on Ruby on Rails and it is a blog! I am following a tutorial
    and have created a comments box which does NOT print the comment on the
    page so far when written in the comments box, that's the problem. I will
    explain step by step I have:

    1. Called out on routes.rb:

    resources :posts do
    resources :comments
    end

    2. Called out using Cmder:
    rails g controller comments create destroy


    3. This is my comments_controller.rb class:

    class CommentsController < ApplicationController
    def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment]) #around here is
    the error
    @comment.save

    redirect_to @post
    end

    def destroy
    end
    end



    can anybody please help?

    --
    Posted via http://www.ruby-forum.com/.
    --
    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/fdeed190-29d5-46d3-ae41-a5199b9575d9%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason Fleetwood-Boldt at Sep 2, 2014 at 4:28 pm
    If you change it to @comment.save! does the response work equally as well or do you get an error.

    Try putting a debugger (see pry for Ruby < 2 or byebug for Ruby 2) line on the line before @comment.save, run the code (actually run the test!) and on the debugger line type "@comment.save" and see what it says.

    On Aug 30, 2014, at 8:40 PM, Mohammed Rashid wrote:

    I am trying to create my first ever program Using Ruby programming
    language on Ruby on Rails and it is a blog! I am following a tutorial
    and have created a comments box which does NOT print the comment on the
    page so far when written in the comments box, that's the problem. I will
    explain step by step I have:

    1. Called out on routes.rb:

    resources :posts do
    resources :comments
    end

    2. Called out using Cmder:
    rails g controller comments create destroy


    3. This is my comments_controller.rb class:

    class CommentsController < ApplicationController
    def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment]) #around here is
    the error
    @comment.save

    redirect_to @post
    end

    def destroy
    end
    end



    can anybody please help?

    --
    Posted via http://www.ruby-forum.com/.

    --
    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/335bf9a03da77a47707ee83e516d92d4%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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/16959D8F-AAAA-4857-8734-1E4D6D225FE1%40datatravels.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mohammed Rashid at Sep 3, 2014 at 1:18 pm
    Thank you for your help guys I have managed to solve the problem. Great
    advice I really appreciate it.

    --
    Posted via http://www.ruby-forum.com/.

    --
    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/a73834fccd71a0b3019b2482a47567e6%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedAug 31, '14 at 12:40a
activeSep 3, '14 at 1:18p
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase