Ruby on Rails Sunday, August 31, 2014

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.

No comments:

Post a Comment