Isaac Y
Isaac Y

Reputation: 660

ActiveAdmin Passing Resource from Controller to Render

I am building a rails application using ActiveAdmin, and want to build a form for a custom action in the controller. I am trying to pass @listings from a collective_action to the rendered file, allowing me to edit multiple records at once. The @listings are based on an ActiveRecord query which draws record IDs from the URL.

It seems to be successfully accessing the parameters from the URL, and querying the database. When the form is served to the browser however, it is not able to produce the values of listing.title. Any ideas?

Here is my Listing.rb:

ActiveAdmin.register Listing do
    collection_action :batch_upload do
     ids = params[:id]
     @listings = []
     @listings = Listing.find(ids)
       render template: 'listings/edit_batch'
    end
end

Here is my edit_batch.html.haml:

= semantic_form_for :listing, :url => {:controller => 'listings', :action => 'batch_upload'}, :html=>{:method=>:put} do |f|
    [email protected] do |listing|
        =f.hidden_field :id, :value => listing.id
        =f.input :title, :value => listing.title
    =f.submit "Submit"

Upvotes: 0

Views: 1140

Answers (3)

Charles Maresh
Charles Maresh

Reputation: 3363

Using Formtastic's inputs block might help simplify the inputs for each listing. Doing so will allow the form to create attribute fields for each Listing object.

# edit_batch.html.haml
= semantic_form_for :listing, :url => {:controller => 'listings', :action => 'batch_upload'}, :html=>{:method=>:put} do |f|

    - @listings.each_with_index do |listing, index|

      f.inputs for: listing, for_options: { index: index } do |l|
        = l.input :id, as: :hidden
        = l.input :title

    = f.submit "Submit"

The :for and :for_options scope inputs fields to a specific object.

Upvotes: 0

Isaac Y
Isaac Y

Reputation: 660

I changed the code to the input so that it accesses its html directly and it worked:

=f.input :title, :input_html => { :value => listing.title }

Upvotes: 1

Tom Kadwill
Tom Kadwill

Reputation: 1478

If the form is correctly displaying listing.id but not listing.title then I suspect the record does not have title set, or listing does not have a title attribute.

To check, run the Rails console and find the record using the id from the form:

$ Listing.find(1)

Check the returned object to see whether it is missing the title.

Upvotes: 1

Related Questions