Creating my first blog with ruby on rails

34 views
Skip to first unread message

Mohammed Rashid

unread,
Aug 30, 2014, 8:40:35 PM8/30/14
to rubyonra...@googlegroups.com
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/.

Richard Wilson

unread,
Sep 1, 2014, 1:23:37 AM9/1/14
to rubyonra...@googlegroups.com
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.

Jason Fleetwood-Boldt

unread,
Sep 2, 2014, 12:28:27 PM9/2/14
to rubyonra...@googlegroups.com


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.
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@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.
>

Mohammed Rashid

unread,
Sep 3, 2014, 9:20:01 AM9/3/14
to rubyonra...@googlegroups.com
Thank you for your help guys I have managed to solve the problem. Great
advice I really appreciate it.
Reply all
Reply to author
Forward
0 new messages