Following up on a previous question, I have a few issues to resolve before I have a comment form showing and submitting securely on my profile. I'm a beginner to programming so thinking across multiple controllers seems to have me lost.
What I'm doing is posting comments in a form, then listing them.
Background: The _comment_form
and _comment
reside as partials in the Profile
about. (My next task is toggling from about to other Profile information, but that's another question altogether.)
Using the help provided in my last question I feel like I'm almost there but am getting an error.
CreateComments migration:
t.integer :profile_id
t.integer :author_id
t.string :body
My Comment model:
class Comment < ActiveRecord::Base
belongs_to :profile
belongs_to :author, :class_name =>"User", :foreign_key => "author_id"
end
CommentsController:
def create
@comment = Comment.new(params[:comment].merge(:author_id => current_user.id))
@comment.save!
redirect_to profile_path(@comment.profile)
end
ProfilesController:
def create
@profile = Profile.new(params[:profile])
if @profile.save
redirect_to profile_path(@profile), :notice => 'User successfully added.'
else
render :action => 'new'
end
end
def show
@user = User.find(params[:id])
@profile = @user.profile
@comment = @profile.comments.new
end
Comment partials inside Profile partial:
<div id="commentEntry">
<%= render :partial => 'comment', :collection => @profile.comments %>
</div>
<div id="newitem">
<%= render :partial => 'comment_form' %>
</div>
Routes.rb:
resources :users do
resources :profiles
end
resources :comments
_comment_form.html.erb:
<%= form_for @comment do |f| %>
<%= f.text_field :body %>
<%= f.submit 'Add new' %>
<% end %>
_comment.html.erb:
<li class="comment开发者_StackOverflow中文版" title="<%= @comment.author.profile.first_name %> <%= @comment.author.profile.last_name %>">
<%= @comment.body %>
</li>
So, Issue #1: Wrapping the _comment.html.erb
in a loop <% for @comment in @user.profile.comments %>
shows the profile but when I try and submit a new comment I get "Unknown action The action 'update' could not be found for CommentsController". If I take away the loop, the profile doesn't show and I get "NoMethodError in Profiles#show undefined method `profile' for nil:NilClass". Can anyone help me out and explain what I'm doing wrong?
Issue #2: I created a sample comment in rails console
and when I get the profile to show, the input field for comment :body repopulates with the comment's body. Any ideas on what could be going on?
Short explanation of your problem:
The @comment
you're getting in your _comment_form
partial is one that's already saved in your database, hence the call to the update
action and the body that's already filled.
You're creating the new comment just fine with @comment = @profile.comments.new
in your show
action, but it gets overridden somewhere else.
You're mentioning that you wrapped the _comment
render in a loop with <% for @comment in @user.profile.comments %>
, the problem is most likely there.
Fix:
The only thing you should have to change is the _comment
partial to (without the for loop that you added):
<li class="comment" title="<%= comment.author.profile.first_name %> <%= comment.author.profile.last_name %>">
<%= comment.body %>
</li>
When you do the render :partial => 'comment', :collection => @profile.comments
, rails is smart enough to loop over @profile.comments
and give the comment
(not @comment
) variable to the partial.
How to avoid this the next time:
I'll give you two rules of thumb to avoid getting in this situation:
Try to name your variables more precisely.
@new_comment
would have been a better name for the variable to store the new comment.@comment
is a bit ambigous as you've got a boatload of those in your view.Avoid creating and modifying instance variables (
@
variables) in your views, try to do this only in your controller. I'll admit your particular case was a bit harder to detect because of the<% for @comment in @user.profile.comments %>
. The view got its name for a good reason, it's only supposed to let you view the data you've defined in your controller.
Hope this helps.
精彩评论