simple_form clickable value using link_to - ruby-on-rails

hopefully a simple question.
Is it possible to make the value of a simple_form field a link_to?
I have tried
= f.input link_to(:project_id,project_path(#project_action_plan.project_id)), readonly: true
and also
= f.input :project_id, :input_html => {:value => link_to #project_action_plan.project_id, project_path(#project_action_plan.project_id)}, readonly: true
But am not getting the desired result.
Google only really brings up this stackoverflow result which was more about then using ajax etc and the simple_form Github doesn't mention anything. I am thinking it may not be possible and that I will have to make the label clickable.

The way the helper methods work is very specific. If you want to write your own custom helper then it's possible to create your own arbitrary formatter methods by creating a subclass of FormBuilder and using that when rendering your forms.
More information on this subject is available in the documentation on Form Helpers.

Related

Simple Form non-model forms and i18n

I'm using simple_form to generate a search form for ransack. The form object passed in isn't an ActiveRecord object and so doesn't respond to object_name. This means I can't use the usual i18n structure for translating labels and hints etc...
simple_form:
labels:
search:
id_gteq: ID greater than or equal to
id_lteq: ID less than or equal to
My current solution is to pollute the default namespace:
simple_form:
labels:
defaults:
id_gteq: ID greater than or equal to
id_lteq: ID less than or equal to
Is there a better way? I thought of monkey patching Ransack::Search to add the object_name method. It would be better if I could pass the object_name through in the simple_form_for call but looking through the code, I don't think that's currently possible.
Here's an example field:
<%= form.input :id_gteq, :required => false %>
Put your tries on file ransack.en.yml, You need to translate the attributes of ransack not simple_form

text_field_tag with ActiveAdmin

I've a hard time with ActiveAdmin and their DSL. I'm using it to build my admin and at some point in a form I need to have a text_field_tag; I mean some fields which aren't related to the model I'm manipulating in the form which will be sent through with the model related data.
A custom text field basically. Nothing too crazy.
So I've built this
panel 'Send payment authorization' do
active_admin_form_for EventPaymentAuthorization.new, url: { action: :send_event_payment_authorization } do |f|
f.inputs do
f.input :body, as: :text
f.text_field_tag :line_items_label
f.text_field_tag :line_items_amount
f.input :fees_in_cents, as: :select, collection: [:free, :automatic], prompt: true, selected: :automatic
end
f.actions do
f.action :submit, label: 'Create payment authorization'
end
end
end
The f.text_field_tag get simply ignored by ActiveAdmin. Why is that? It doesn't raise any error, but it doesn't show either.
The reason I need custom unrelated inputs is because line_items in my example is a JSONB with values such as [{amount: 0.0, label: 'Hello'}] and I don't believe it can be processed through Formtastic or ActiveAdmin natively. It's also always good to be able to create custom inputs when sending data.
Working with JSON in ActiveAdmin is a bit tricky, it largely depends on your needs. The quickest way to get going is to use the activeadmin_json_editor gem. I also wrote a blog entry on working with JSON in ActiveAdmin with more detail and another approach, which may better suit your needs, as it appears you are not just working with arbitrary data into your JSON field.

SimpleForm remove wrapper divs

I'm using SimpleForm in my Ruby on Rails app. To make my input look good with my current css I need to disable wrapper every time for the input:
f.input :email, wrapper: false, input_html: { class: 'input' }
How can I remove this wrapper globally?
If by "globally" you mean all forms in application, then I don't think it can be done through standard configuration in config/initializers/simple_form.rb. However, you can easily disable wrappers in a form by setting defaults for all inputs in it:
<%= simple_form_for #user, defaults: { wrapper: false } do |f| %>
Please remember that wrappers in simple_form are highly configurable and controlling classes for them is possible with options available in initializer.

Rails Action View Form Helper pass options hash to create element

Given a hash of properties for a text_field to be made in HAML:
entry = {class: "form-control", disabled: true, id: :foobar}
How can I do this:
%dd!= f.send(:text_field, entry[:id], class: entry[:class], disabled: entry[:disabled])
But flexible? (So don't need placeholders, just a hash). Have looked at simple form docs and action view form helper docs and found input_html, but that's not working in this manner.
The f.send is obligatory, as :text_field can be anything. Would prefer not to use eval
text_field takes:
text_field(attribute_name, input_html_options)
Does
f.text_field(entry[:id], entry)
not work?

Formtastic non-model form, integration with external site, override/specify the input ID values

I'm using formtastic to collect information from a form and post dirctly to an external site.
I have no problem generating the form itself. However, since this is being submitted to an external site, they require that each input field have the specific IDs they specify, eg email or last_name -- not the closest Formtastic form, eg _email_input or _last_name_input.
I've looked at the Formtastic v1.2.3 code and I'm 90% sure the answer is "sorry, can't do that." I figured it couldn't hurt to check if I'm missing something. I would like some way to specify the ID completely, as in:
= semantic_form_for('', :url => "https://external_site.com/handler, :method => "post") do |form|
= form.input :last_name, :id => "last_name"
[etc]
Is this possible?
(I will note that I recognize that another, arguably superior approach would be to create an appropriate controller, sanity check the parameters locally, and dispatch the remote call from within the app only when it's well formed; however, that's not what I'm trying to do at the moment.)
Firstly i think you need to use semantic_fields_for for non-model forms. Next, to pass ids to each field, you can use the input_html options to specify them. for eg
form.input :email, :input_html => {:name => 'email', :id => 'email' }

Resources