Rails 3 - 嵌套表单和默认值

我正在尝试创建包含rails中另一个模型的表单。我使用accepts_nested_attibutes完成了这项工作,并且它运行良好。问题是我在该表中有一个额外的字段记录每个注释的用户名,我不确定如何在创建新注释时插入该信息。应用程序控制器使用“current_user”方法提供用户名。 问候, 凯尔 评论模型
    class Comment < ActiveRecord::Base
     belongs_to :post
     before_save :set_username

    private
     def set_username
      self.created_by = current_user
     end
    end
应用程序控制器(这只是一个沙盒应用程序所以我只是在方法中放入一个字符串)
class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :current_user

  def current_user
    "FName LName"
  end

end
显示视图
<p id="notice"><%= notice %></p>
<p>
  <b>Title:</b>
  <%= @post.title %>
</p>
<div id="show_comments"><%= render 'comments' %></div>
<div id="add_comments">
    Add Comment
    <%= form_for @post, :url => {:action => 'update', :id => @post.id}, :html => { :'data-type' => 'html', :id => 'create_comment_form' } do |f| %>
        <%= f.fields_for :comments, @new_comment do |comment_fields| %>
            <%= comment_fields.text_area :content %>
        <%end%>
      <div class="validation-error"></div>
      <%= f.submit %>
    <% end %>
</div>
后控制器
  def update
    @post = Post.find(params[:id])
    respond_to do |format|
      if @post.update_attributes(params[:post])
        @comments = @post.comments.all
        format.html { redirect_to({:action => :show, :id => @post.id}, :notice => 'Post was successfully created.') }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end
    
已邀请:
我原本以为你可以将它设置为模型中的默认值或before_save。但模型无法访问
current_user
。因此,最好只在控制器中设置当前用户。它并不像把它放在模型中那样干,但它不那么hackey并且这种方式可能有问题。
def update
  @post = Post.find(params[:id])
  @post.attributes = params[:post]
  @post.comments.each do |comment|
    comment.created_by = current_user if comment.new_record?
  end
  respond_to do |format|
    if @post.save
      @comments = @post.comments.all
      format.html { redirect_to({:action => :show, :id => @post.id}, :notice => 'Post was successfully created.') }
      format.xml  { render :xml => @post, :status => :created, :location => @post }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
    end
  end
end
    
只是想指出可以在模型范围内访问
current_user
。在这种情况下,我认为这是必要的,因为@aNoble的解决方案应该有效。因此,如果可以从控制器设置current_user,我宁愿这样做。 简而言之,我们在
User
类中添加了一个方法
class User < ActiveRecord::Base
  cattr_accessor :current_user

  def self.current_user
    @current_user ||= User.new("dummy-user")
  end
  ...
end
在您的应用程序控制器中,我们添加一个设置它的
before_filter
。验证完成后,请务必调用此过滤器。
class ApplicationController < ActionController::Base

  before_filter { |c| User.current_user = current_user }

end
而且,然后在你的
Comment
模型中你可以做类似的事情
class Comment

  before_create :set_user

  def set_user
    created_by = User.current_user unless created_by
  end
end
(所以我只设置了
created_by
,如果还没有设置,只有在创建新评论时)。     

要回复问题请先登录注册