Search code examples
ruby-on-railsactiverecorddeviseformtasticactiveadmin

Using Devise Current User as default user in User Select box


I'm using Devise and Active Admin, and so far there are playing nice together. Instead of using the default Active Admin "AdminUser', I am using my own 'User' model, which is sometimes refered to as 'Author' within my app.

I'm creating an Article form (Create/Update) and I have a User Select Box that is pre-selected if the record is an update, but not if it is a new record. What I want to do is if I am creating a new record, use the Devise' 'current_user'. Since ActiveAdmin is using Formtastic, I found this: https://github.com/justinfrench/formtastic/wiki/Deprecation-of-%3Aselected-option, which basically explains that might be a feature coming, but that this might be best achieved with a before filter, which is where I am stumped now.

Here is my form view:

<%= semantic_form_for [:admin, @article] do |f| %>
    <%= f.inputs "Details" do %> 
        <%= f.input :title %>
        <%=
            f.input :user, :as => :select, :label => 'Author',
            :collection => User.all, :include_blank => false,
            :required => true 
        %>

        <%=
            f.input :categories, :as => :check_boxes, :label => 'Categories',
            :collection => Category.all, :include_blank => 'None',
            :hint => 'Choose all categories that apply.',
            :required => false 
        %>

        <%= f.input :desc, :label => 'Description' %>
        <%= f.input :content, :label => 'Content' %>
    <% end %>
    <%= f.buttons %>
<% end %>

I can pre-selct the current_user using jQuery, something like this:

<script type="text/javascript">
  $(document).ready(function() {
    $('#article_user_id').val(<%= current_user.id %>);
  });
</script>

but this overrides the selected user in the 'Edit' view, in which the Author/User has already been assigned... and it also doesn't seem like Rails way...

Edit: I found this: Active Admin: Customize only new form

So based on that, in my view I determine if the Article is a new record or not, and if it is, I wrap the javascript so that it gets put on the page for new articles. This way, on article updates, the user is still selected.

<% if controller.action_name == 'new' %>
  <script type="text/javascript">
  $(document).ready(function() {
    $('#article_user_id').val(<%= current_user.id %>);
  });
  </script>
<% end %>

Again, I don't think this is the most elegant solution, so if anyone has any comments on a better way (which I'm sure there is), please let me know!


Solution

  • Use a before_filter in your controller...

    controller do
      before_filter :assign_user, :only => :new
    
      private
    
      def assign_user
        resource.user = current_user
      end
    end
    

    This way, when your form is rendered, @article will already have the user id populated via the call to the resource method (which references @article in your case).