获取个人资料以更新并显示新提交的表单项时出现问题 [英] Issues getting a profile to update and show newly submitted form item

查看:25
本文介绍了获取个人资料以更新并显示新提交的表单项时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注上一个问题,我需要先解决一些问题,然后才能在我的个人资料上显示并安全提交评论表单.我是编程的初学者,因此跨多个控制器进行思考似乎使我迷失了.

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.

背景: _comment_form _comment 作为部分内容存在于 Profile 中.(我的下一个任务是从切换到其他个人资料信息,但这是

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迁移:

CreateComments migration:

t.integer :profile_id
t.integer :author_id
t.string :body

我的评论模型:

class Comment < ActiveRecord::Base
  belongs_to :profile
  belongs_to :author, :class_name =>"User", :foreign_key => "author_id"
end

CommentsController:

CommentsController:

def create
  @comment = Comment.new(params[:comment].merge(:author_id => current_user.id))
  @comment.save!
  redirect_to profile_path(@comment.profile)
end

ProfilesController:

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:

Routes.rb:

resources :users do
  resources :profiles
end
resources :comments

_comment_form.html.erb:

_comment_form.html.erb:

<%= form_for @comment do |f| %>
  <%= f.text_field :body %>
  <%= f.submit 'Add new' %>
<% end %>

_comment.html.erb:

_comment.html.erb:

<li class="comment" title="<%= @comment.author.profile.first_name %> <%= @comment.author.profile.last_name %>">
  <%= @comment.body %>
</li>

因此,问题#1: _comment.html.erb 循环包装在 profile.comments中的@comment%> 显示了个人资料,但是当我尝试提交新评论时,我收到未知动作无法为CommentsController找到动作'更新'".如果取消循环,则不会显示配置文件,并且会收到"Profiles#show nil:NilClass的未定义方法`profile'中的NoMethodError".谁能帮助我并解释我做错了什么?

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?

问题2::我在 rails控制台中创建了一个示例注释,当我显示要显示的配置文件时,注释的输入字段:body用注释的正文重新填充.关于可能发生的事情有什么想法吗?

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?

推荐答案

问题的简短说明:

您在 _comment_form 部分中获得的 @comment 是已保存在数据库中的部分,因此调用了 update 动作和已经充满的身体.

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.

您正在 show 动作中使用 @comment = @ profile.comments.new 来创建新注释,但是它将被其他地方覆盖.

You're creating the new comment just fine with @comment = @profile.comments.new in your show action, but it gets overridden somewhere else.

您要提到的是,您将问题 _comment 渲染器与@ user.profile.comments%> 中@comment的<%一起包装在循环中最有可能在那里.

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.

修复:

您唯一需要更改的是 _comment 部分(没有添加的for循环):

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>

执行 render:partial =>时,'comment',:collection =>@ profile.comments ,rails足够聪明,可以遍历 @ profile.comments 并给出 comment ( not @comment )部分变量.

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.

下次如何避免这种情况:

为了避免出现这种情况,我会给您两个经验法则:

I'll give you two rules of thumb to avoid getting in this situation:

  1. 尝试更精确地命名变量.对于存储新注释的变量, @new_comment 会是一个更好的名称. @comment 有点模棱两可,因为您在视图中看到了很多内容.

  1. 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.

避免在您的视图中创建和修改实例变量( @ 变量),请尝试仅在控制器中执行此操作.我承认,由于@ user.profile.comments%> 中@comment的<%,因此您的特殊情况很难检测.视图之所以命名是有充分的理由的,它只能让您查看在控制器中定义的数据.

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.

希望这会有所帮助.

这篇关于获取个人资料以更新并显示新提交的表单项时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆