Comment attribute won't save in Rails? - ruby-on-rails

I'm trying to save the comment text section of a comment and it won't save for some reason.
I'm checking my server outputs, and the comment attribute is set, but when actually saved, it turns out as NIL.
Originally it has "comment"=>{"comment"=>"hello dude"}, "commit"=>"Sbmit Comment"
But in the saving it saves NIL.
Here's my form for comments
<div class="container">
<% if user_signed_in? %>
<%= form_for([#answer, #comment]) do |f| %>
<p>
<%= f.label :comment %>
<%= f.text_area :comment, :cols => "50", :rows => "30"%>
</p>
<p>
<%= f.submit "Submit Comment" %>
</p>
<% end %>
<% else %>
<p> <em>You must be signed in to comment</em> </p>
<% end %>
</div>
Here's my comments controller
class CommentsController < ApplicationController
def create
#answer = Answer.find(params[:answer_id])
#comment = #answer.comments.new(params[:comments])
#comment.writer = current_user.username
#comment.save
redirect_to question_path(#answer)
end
end
And here's my model.
class Comment < ActiveRecord::Base
belongs_to :answer
attr_accessible :anonymous, :comment, :writer, :votes
end
Heres my answers model
class Answer < ActiveRecord::Base
has_many :comments, dependent: :destroy
belongs_to :question
attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes
end
Any ideas?
EDIT: I've used params[:comment], however, it says I cannot mass assign attributes to answer, even though answer has attr_accessible: :comments_attributes

In this line #comment = #answer.comments.new(params[:comments]) change :comments to :comment

You are trying to save with params[:comments] in your controller but what is being passed would respond to params[:comment]

Related

Some attributes of model saving but others not?

I'm creating an an answer model and the query portion of it is saved, however, the anonymous portion of it is not.
This is my answers model
class Answer < ActiveRecord::Base
has_many :comments, dependent: :destroy
belongs_to :question
attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes
accepts_nested_attributes_for :comments
end
And this is my form.
<%= form_for([#question, #answer]) do |f| %>
<p>
<%= f.label :answer %>
<%= f.text_area :answer, :cols => "50", :rows => "30"%>
</p>
<p>
<%= check_box_tag(:anonymous)%>
<%= label_tag(:anonymous, "Anonymous")%>
</p>
<p>
<%= f.submit "Submit Answer" %>
</p>
<% end %>
When I check my command prompt for the POST request, initially both answer and anonymous have values, answer may have string "aw;oeigh;aioewhg" and anonymous has value 1. However, when it does the actual create, anonymous gets nil and answer gets "a;oewigh;oih." Why is this
This is my controller if it's any help. Thanks!
def create
#answer = #question.answers.new(params[:answer])
if #answer.anonymous == true
#answer.commenter = "Anonymous"
else
#answer.commenter = current_user.username
end
if #answer.save
redirect_to question_path(#question)
else
redirect_to questions_path
end
end
it may be because your params anonymous is not within answer
try
f.check_box :anonymous
instead
<%= check_box_tag(:anonymous)%>

Can't assign mass attributes to comments?

Can't mass-assign protected attributes: answer. (Using Rails3)
I'm not sure why it's not allowing me to do so as I have my nested attributes accessible.
This is my answer model
class Answer < ActiveRecord::Base
has_many :comments, dependent: :destroy
belongs_to :question
attr_accessible :anonymous, :answer, :commenter, :votes, :comments_attributes
accepts_nested_attributes_for :comments
end
This is my comments model
class Comment < ActiveRecord::Base
belongs_to :answer
attr_accessible :anonymous, :comment, :writer, :votes
end
I'm failing at this form on the view
<%= form_for([#answer, #comment]) do |f| %>
<p>
<%= f.label :comment %>
<%= f.text_area :comment, :cols => "50", :rows => "30"%>
</p>
<p>
<%= f.submit "Submit Comment" %>
</p>
<% end %>
This is my function in my commentsController that is apparently causing the error
def create
#answer = Answer.find(params[:answer_id])
#comment = #answer.comments.new(params[:comment])
#comment.save
redirect_to question_path(#answer)
end
Your view code is not technically right. You need to use fields_for:
<%= form_for([#answer, #comment]) do |f| %>
<p>
<%= f.fields_for :comments do |u| %>
<%= u.label :comment %>
<%= u.text_area :comment, :cols => "50", :rows => "30"%>
<% end %>
</p>
<p>
<%= f.submit "Submit Comment" %>
</p>
<% end %>
you may also need to remove #comment from the form_For helper. In fields_For, you might need to user :comment or possibly even #comment.
If you are using rails 4, then you will also have a problem with strong parameters: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

Rails: belongs_to & accepts_nested_attributes_for, polymorphic

I'm working on my first Rails project, and I have the following model relationship:
class Profile < ActiveRecord::Base
belongs_to :identifiable, polymorphic: true
accepts_nested_attributes_for :students
class Student < ActiveRecord::Base
has_one :profile, as: :identifiable
attr_accessible :profile
The associated controllers are:
class StudentsController < ApplicationController
def new
#student = Student.new
end
def create
#student = Student.new(params[:student])
if #student.save
redirect_to root_path
else
render 'new'
end
end
end
And
class ProfilesController < ApplicationController
def new
#profile = Profile.new
end
def create
#profile = Profile.new(params[:profile])
#profile.save
end
end
What I'm trying to do is create a new Student with the following form, which is in students\new.html.erb:
<h1>Create a new Student Account</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#student) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.fields_for :profile, #profile do |builder| %>
<%= builder.label :name %>
<%= builder.text_field :name %>
<%= builder.label :email %>
<%= builder.text_field :email %>
<%= builder.label :password %>
<%= builder.password_field :password %>
<%= builder.label :password_confirmation, "Confirmation" %>
<%= builder.password_field :password_confirmation %>
<% end %>
</div>
</div>
<p><%= f.submit "Submit", class: "btn btn-large btn-primary" %></p>
<% end %>
I'm getting the following error message when I try to submit the form: No association found for name 'students'. Has it been defined yet? What am I doing wrong? Thanks in advance.
In order for a model to accept nested attributes for another model, the association to the other model needs to be declared. In Profile you have accepts_nested_attributes_for :students, but there is no corresponding association defined (e.g. has_many :students), which is why you're getting that particular error. In your case, this association wouldn't be correct, however.
Usually, if model A accepts nested attributes for model B, either A has_many B or A has_one B. In your case, you have A belongs_to B. A better design would be
class Profile < ActiveRecord::Base
belongs_to :identifiable, polymorphic: true
class Student < ActiveRecord::Base
attr_accessible :profile_attributes
has_one :profile, as: :identifiable
accepts_nested_attributes_for :profile
Should your student be singular? ie: accepts_nested_attributes_for :student
Edit: Also, your Student should accept nested attributes for a Profile, if the Student has_one profile, and the Student form contains the fields_for call (I think...)

Rails: Comment Model - Connected to a Micropost_id and User_id

EDIT
The Routing Error has disappeared thanks to carlosramireziii who helped changed the Form to
<%= form_for (#micropost) do |f| %>
<%= fields_for :comments do |ff| %>
<%= ff.text_area :content %>
<% end %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>
But the issue now is that the post will not save, any suggestions?
I am currently making a comments model that is connected to a:
micropost_id :integer and user_id :integer
The problem that I continue to receive is when I post something I get this in return:
Routing Error
No route matches [POST] "/microposts/comments"
This is my routes.eb
Project::Application.routes.draw do
resources :microposts do
resources :comments
end
This is the Comment Form
<%= form_for([#micropost, #micropost.comments.new]) do |f| %>
<%= f.text_area :content %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>
This is the Comment Template
<%= div_for comment do %>
<div class='UserCommentContainer'>
<div class='UserComment'>
<div class='UserName sm'>
Anonymous
</div>
<div class='UserCommentText'>
<%= comment.content %>
</div>
</div>
</div>
<% end %>
And finally this is what is inside the Micropost
<div id='CommentContainer' class='Condensed2'>
<div class='Comment'>
<%= render "comments/form" %>
</div>
<div id='comments'>
<%= render #micropost.comments %>
</div>
</div>
Everything else related to the comments model and controller I have posted below, I have been mulling around this for a long time and could really use help, thank you!
Comments Model
class Comment < ActiveRecord::Base
attr_accessible :content
belongs_to :micropost
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
validates :micropost_id, presence: true
default_scope order: 'comments.created_at DESC'
end
Micropost Model
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
validates :user_id, presence: true
end
User Model
class User < ActiveRecord::Base
has_many :microposts
has_many :replies, :through => :microposts, :source => :comments
end
Comments Controller
class CommentsController < ApplicationController
def create
#comment = #micropost.comments.new(params[:comment])
if #comment.save
redirect_to #user
else
redirect_to #user
end
end
end
User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#micropost = Micropost.new
#comment = #micropost.comments.new
#microposts = #user.microposts.paginate(page: params[:page])
end
end
I believe the problem is that you are trying to save a new comment on a micropost that hasn't been saved yet. Since you have your comments routes nested underneath the microposts routes, the micropost needs to exist before you can create a new comment.
If you want to create both objects in the same form, you need to use nested model attributes.
Micropost
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
accepts_nested_attributes_for :comments
validates :user_id, presence: true
end
Form
<%= form_for(#micropost) do |f| %>
<%= f.fields_for :comments do |ff %>
<%= ff.text_area :content %>
<% end %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>

how to check if parent exists in has many relation

I have 3 models:
User: has_many :comments
Video: has_many :comments
Comment: belongs_to :video, :user
videos/show.html.erb
<%= form_for(#comment, :method => :post ) do |f| %>
<%= f.text_area :body %>
<%= f.hidden_field :video_id, :value =>"4" %>
<%= f.submit "submit" %>
<% end %>
comments_controller
def create
#comment = Comment.new(params[:comment].merge(:user_id => current_user.id))
#comment.save
end
How can i check existence of Video before i create a comment?
This should be present as a validation in your Comment model.
class Comment < ActiveRecord::Base
validates_presence_of :video

Resources