简体   繁体   中英

ActiveAdmin passing variable in controller

I have a permit and vehicle model. I am trying to update the AA create controller to work how I have it in my rails app. That is taking the vehicle license_number entered and inputting it into the permit table, then also taking the inputted permit_id and inputting it into the permits attribute of the vehicle it is related to in the vehicle table.

admin/permit.rb

permit_params :permit_id, :vehicle, :date_issued, :issued_by, :date_entered, :entered_by

form do |f|
    f.inputs do
        f.input :permit_id
        f.input :vehicle, :collection => Vehicle.all.map{ |vehicle| [vehicle.license_number]}
        f.input :date_issued, as: :date_picker
        f.input :issued_by
    end
    f.actions
end

controller do
    def new
        @permit = Permit.new
        @vehicle = @permit.build_vehicle
        @vehicle = Vehicle.all 
        super
    end 

    def create
         vehicle = Vehicle.find_by(license_number: permit_params[:vehicle_attributes][:license_number])
         @permit = current_user.permit.build(permit_params.merge(date_entered: Date.today, 
            entered_by: current_user_admin.email))
         super
    end
end

My errors that I am getting, is that it is inputting the license_number in for the permit_id and then it is also saying the permit_params is not a defined variable. Any help would be great, thanks!

You have an interesting case here: it is confusing because you have a model called Permit , and usually in Rails you name the params method something like permit_params . Turns out, permit_params is the general method signature for ActiveRecord to implement strong params: https://activeadmin.info/2-resource-customization.html

With that, instead of calling permit_params in your create action, you need to call permitted_params[:vehicle_attributes][:license_number] . That's why it's considering permit_params as an undefined variable. Again, those two method signatures will be the same for all your ActiveAdmin forms.

Additionally, I'm not sure if this is a typo but you define @vehicle twice in your new method. I'm not sure you need to build a vehicle for the permit form unless you're doing nested forms. Either way, I think the last line should read @vehicles = Vehicle.all Then you can use that in your form, which also could use an update in the collection part of your form field:

form do |f|
  f.inputs do
    f.input :permit_id
    f.input :vehicle, collection: @vehicles.map { |vehicle| [vehicle.license_number, vehicle.id] }
    f.input :date_issued, as: :date_picker
    f.input :issued_by
  end
  f.actions
end

The collection_select form tag will take the first item in the array as the value that appears in the form, and the second value will be the value passed through in the params ( https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select ).

Then in your create action, you can find the Vehicle with the id:

Vehicle.find(permitted_params[:vehicle_attributes][:vehicle_id])

我会避免将Permit作为模型名称,请尝试使用VehiclePermit。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM