Activeadmin set resource's attributes on action - ruby-on-rails

I want to set a default value for an attribute of the activeadmin resource, so when it renders the form, it will use this value.
e.g.: in the action new, I want to set the attribute product of the object order , so the form will come with the product already selected(but leting it to be changed).

Just add the value to the form field as follows:
f.inputs do
f.input :product, input_html: { value: products_value }

I accepted Andrey Deineko's answer, but even before I saw it, I've done it in another way:
controller do
def new
#resource_name = ResourceName.new(...)
so, I just defined the action method inside the block controller, creating an instance of the resource with an instance variable named as the ActiveAdmin Resource name.

Related

Best way to save the model to use later as "template"

I don't know if template is the right word for this.
I want models which has template boolean true, to be choose-able at the same models form view. And on choose it would fill the form with the chosen models values.
I'm thinking about this solution:
List the models next to he form which has the template attribute set to true.
On click load it trough AJAX , and fill the form with the loaded attributes trough javascript.
I'm wondering if there is a better way for this?
Edit.:
Sorry if it wasn't understandable.
Model.rb has boolean attribute :template.
If template is set to true. It is displayed on the form view of the Model.
_form.html.haml:
:collection_select Model.where(:template => true)
and on select, the template model fills, in the new Model's attributes, with the old template Model's attributes.
I would like to find the Rails way for this.
Add a class method in your model to find the template record.
def self.find_template_record
template = where(template: true).first
raise "no template found" if template.nil?
return template
end
In your controller, load the template record and clone it. Don't use dup because that will copy the id.
def new
#model = Model.find_template_record.clone
end
To duplicate an ActiveRecord model, use its dup method:
#model = #template_model.dup # create the base
#model.attributes = params[:model] # override particular attributes

Ruby on Rails, nested resource, use url id on submit

So I have:
/projects/1/steps/new
When I submit a step, how do I save the project_id in step?
Do I need a hidden form field with "project_id", or can I set some other way?
As of now when submitted project_id get sets to nil
Make sure you are building the form as follows, to maintain the nested routing:
form_for [#project, #step]
Then, inside your StepsController you will receive a parameter :project_id.
HTH.
You can write in your controller create action:
#project.find params[:project_id]
#project.build params[:step]
In that case you don't need to pass hidden field.

Calling a method defined in a different controller when submitted form in Rails

I have a form_for tag specified as = form_for [#driver,#driver_availability].This stores the entered data in the driver_availabilities model and calls the create method of the DriversController.
Is it possible to make it call some method i define in a different controller but continue saving data in the driver_availabilities model as usual ?
Thank You
[#driver, #driver_availability] will call driver_driver_availabilities(driver_id: #driver) for new objects and driver_driver_availability(driver_id: #driver, id: #driver_availability) for existing driver availablities. So either you create a named route routing to the other controller (notice that these named routes are used for index, show, update and destroy as well) or you provide the url option to the form tag:
= form_for [#driver,#driver_availability], url: … # named route or routing hash
I would go with the second option.

Drop down select ruby rails

Here's my code for a form that contains a drop down list -
<div class="field">
<%= f.label :type, "Select profile type"%>
<%=
f.select :type, Profile::TYPES,
:prompt => "Select a profile type"
%>
</div>
The drop down menu looks fine. But, how would I check which option is selected? I want to route to a different view based on this selection.
Thanks in advance!
The logic of routing to a different view should occur in your controller. When the user submits this form, check the value of the params, and perform your logic to route to a view:
class ExampleController
def routing
case params[:example][:type]
when 'foo'
redirect_to foo_path
when 'bar'
redirect_to bar_path
end
end
You can create a custom action name, since this routing isn't one of the CRUD operations. You will need to place this route into the config/routes.rb file if it is a custom name.
Optionally, you can bind to the select's onChange event, as mentioned by others to auto-submit the form when the user changes the value. This would still send the data to the controller and perform a redirect. The advantage to this approach is that you can keep your route information out of Javascript, and in the Rail's controller.
More on Rails routing can be found here: http://guides.rubyonrails.org/routing.html
More on Javascript binding to onChange can be found here: http://www.w3schools.com/jsref/event_onchange.asp
You should be able to use jQuery or any other popular Javascript framework to achieve this -- either attach an onChange listener or set the value somewhere and check it. Events, yay, etc.

Submitting custom information to a controller

This is probably simple, but I've tried a few things and couldn't find a way to make it work.
I would like to update a model with custom information given in a form_for
To make it more concrete, I'm on the show page for a particular instance of MyClass and I would like to pass something like the string "yay" into the controller, and then do as I please with the input. Maybe pass it back to the page as a flash message, or maybe modify the contents and then store it as a field of the MyClass instance.
I can write form_for's that contain the attributes of MyClass without prbolems, but it seems that other fields throw an error.
How do I write the form_for so that I can accomplish one of the two above scenarios?
def update
#my_class = MyClass.find(params[:id])
flash[:notice] = "This works" # but what can I write in a form for for it to be a variable that's passed in?
#rest of the update
end
Form helpers that unitize a form builder instance (like f.text_field) expect a valid model attribute so it can generate the appropriate id and populate the field with data from the model. If you want to have form fields that do not correspond to model attributes, don't use the the standard f.text_field but instead use:
<%= text_field_tag 'my_custom_tag' %>
which should render something like:
<input type="text" id="my_custom_tag"></input>
When the form is submitted, the value of the input will show up in the params hash with a key of :my_custom_tag.
I hope this helps.
It seems that you would probably need a hidden_field in your form :
http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field
However, if you wish to save some kind of state, which seems like this is what you want, you would never use that. Instead, you would use a session. The reason is that a hidden field can be manipulated by the client and thus security can easily be overridden.
Like Spyros said, a hidden field will give you the place. Assuming you are ok with the fact that a user can modify the URL, just add attr_accessor :foo to your model.
In the controller you can access it with bar = params[:foo] and do as you please.

Resources