Condtionally hide Edit action for resource in ActiveAdmin - ruby-on-rails-3

Is there any way to conditionally hide the 'Edit' link per row on #index of a resource?
For example, say you have a User resource. While a User is active, you can edit the User. However, once the User deactivates their account, it should no longer be editable. The User should still show up on the index page with the View link still.
Thanks!

You can make your column instead of default_actions column:
index do
column :actions do |resource|
links = link_to I18n.t('active_admin.view'), resource_path(resource)
if resource.is_active?
links += link_to I18n.t('active_admin.edit'), edit_resource_path(resource)
end
links
end
end

Related

Hidden Some field in .asp

Currently I have to hid a table that cannot view for some user. I'm very new in this .asp. I'm not sure how to do it in the coding as this coding not build by me.
As image below, i need to hid it from some user.
enter image description here
In this page, there have another table that can view by all user when they login and access to this page. Some one please me some idea. :)
Below the part of the table coding that i need to touch for me to hid it.
Blockquote
Add Self-Collect DN
" />
"" then response.write "This DO-"&do2& " had been block!" end if%>
DN-
Saleman ID
Customer Account
Blockquote
You can manipulate the table by assigning an id.
<table id="IdHideShow" runat="server">
now in your code behind, you can manipulate the visibility of the table by the use of its id , depending on how you want it.
something like:
If SomeBooleanConditionHere = True Then
IdHideShow.Visible = True
Else
IdHideShow.Visible = False
End If

How to generate adidional fields inside Yii form

I have:
Table "user"
id, username, password
Table "freedate"
id, user_id, start_date, end_date
Each user can have 1 or more (unlimited) number of "freedate" entries.
Now i have form with 2 text fields (username and password), and 2 date pickers (one for start date and another one for end date).
How can i enable user to inserd aditional datepicker pairs so they can enter as much of "freedate" entires as they need to?
I was wondering about insertind aditional button inside form, that would somehow create aditional fields when pressed, but u have no idea how to do it.
I don't need working example (even tho one would help ofc). What i need i suggestion from your own expirience if you had similar problem.
You can use javascript as noted above. Though that can get tricky to get the new fields associated with the datepicker.
You can also submit after each pair is entered. On returning back to form after save insert a new set of start/end date fields to the end of the form. This way they always have a freedate pair they can enter. (a bit slower overall)
You need javascript to generate these fields on the fly. Or a fixed amount of extra fields could be hidden and shown one by one using javascript as the user clicks the button. You would need to keep track of the number of fields being shown in a javascript var.
You need to write custom javascript in order to do that. It isn't hard, you would need to do something along these lines:
You need to create a button and, when that button gets clicked (onClick event or jquery click() function) you can add the html for your field (<input type=.... /> and so on to your form) and remember to use the correct name for these fields so that they get sent correctly when the user submits the form.

Rails date field not removing date when updating

I have a form with a text field for inputting a date:
<%= f.text_field :my_date %>
If I input a date into this field, click submit and then go back to the form again and remove the date value that I've just entered (with no validation applied) the form saves but the original date is still there. I can't get rid of it once I've entered and saved it.
Am I doing something wrong?
It was because it is a virtual attribute and needed to use "" if it thought it was nil

Rails/Active Admin/Formtastic - Dropdown cleared on fail validation?

When I select a option from a dropdown on the new/edit view and submit the form to the create/update action, if another field within the same form fails the validation process when the view is returned the option selected from the dropdown has been emptied, this isn't a big problem in the new view but definitely in the edit view.
This is the code for the dropdown to be populated using formtastic collection method:
f.input :ethnic_category, :label => "Ethnic Origin", :collection => Hash[EthnicCat.all.map{|b| [b.ethnic_cat_name, b.id]}]
Any help would be greatly appreciated, Thanks....

I need a button on the edit page to change attribute value

I am new to Rails. In my project, I have a task controller and a tasks table with a column name task_status. When task is saved, the status is set to open. Now, I want to place a mark closed button on the edit view page to set the status to closed.
You can do this in several ways. One way would be to include a check box, named close_task in your form. When this check box is checked and the form is submitted, you check for param[:close_task].
So for example:
def update
if param[:close_task]
#task.status = 'closed'
#task.save
end
...
end