Rails 3 - Nested Forms and Default Values


I am trying to create a form containing a different model in rails. I accomplished this with accepts_nested_attibutes and it works fine. The problem is that I have an extra field in this table that records the username for each comment, and I'm not sure how to insert this information when creating a new comment. The username is provided by the application controller using the current_user method.

Yours faithfully,
Kyle

Comment Model

class Comment < ActiveRecord::Base belongs_to :post before_save :set_username private def set_username self.created_by = current_user end end 

Application Controller (this is just a sandbox application, so I just put a line in the method)

 class ApplicationController < ActionController::Base protect_from_forgery helper_method :current_user def current_user "FName LName" end end 

Show view

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

Mail controller

  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 
+4
source share
2 answers

I initially thought you could just set it as default or before_save in the model. But models do not have access to current_user . Therefore, it is best to simply install the current user in the controller. It is not as dry as putting it in a model, but it is less hacks and potentially problematic.

 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 
+4
source

Just want to point out that you can access current_user in the model area. In this case, I do not think it is necessary, since the solution from @aNoble should work. Therefore, if it is possible to set current_user from the controller, I would prefer this.

In short, we add a method to the User class

 class User < ActiveRecord::Base cattr_accessor :current_user def self.current_user @current_user ||= User.new("dummy-user") end ... end 

and in your controllor application, add before_filter , which sets it. Be sure to call this filter after authentication is complete.

 class ApplicationController < ActionController::Base before_filter { |c| User.current_user = current_user } end 

And then inside your Comment model you can just do something like

 class Comment before_create :set_user def set_user created_by = User.current_user unless created_by end end 

(therefore, I only set created_by if it is not already set, and only after creating a new comment).

0
source

Source: https://habr.com/ru/post/1347296/


All Articles