Suggestion: label input #128

Closed
jduarte opened this Issue Nov 21, 2009 · 5 comments

Comments

Projects
None yet
2 participants

jduarte commented Nov 21, 2009

Hi,

I would just like to post a suggestion because it happens many times in my code and I think on others people code as well.
Having something like a user belongs_to role. In new.html.erb using formtastic will have something like

<%= f.input :role %>

that will generate a select list with all available roles.
But now let's say that both admin and supervisor are able to create users. But if, in our spec, a supervisor is able to create only users with operational role we will need something like:

<% if @user.admin? %>
<%= f.input :role %>
<% elsif @user.supervisor? %>
<% operational = Role.find_by_name('operational').id %>
<%= f.input :role_id, :as => :hidden, :value => operational %>
<% end %>

But now I have no way to show to the user that the Role of the User he is creating is 'operational'.
My suggestion is that we should be able to do something like:

<%= f.input :role, :labelize => { :when => @user.supervisor?, :with { :value => Role.find_by_name('operational').id, :display => Role.find_by_name('operational').name } } %>

and this piece of code would generate an li tag with

  • hidden input that would pass the value to the controller
  • label (just like when we do <%= f.input :role %> )
  • label that shows the name of the role

This issue happened to me several times in my projects because we have a constant concern in showing to the user the details of the object he is creating or updating even if he doesn't have permission to change it.

What do you think?
Cheers

Owner

justinfrench commented Nov 21, 2009

I don't think this is a common case for which Formtastic should provide a solution sorry. Besides, I actually find your first example with the if...elsif...end far more readible. Closing this.

jduarte commented Nov 21, 2009

Hi,

I understand that it can be more readable on the if elsif approach but it doesn't show the role on the page in the first example, it only puts the role hidden.

Even if it sticks with the if elsif approach don't you think that it should be an "input" type label just to show to the user what he is currently doing like:

<% if @user.admin? %>
<%= f.input :role %>
<% elsif @user.supervisor? %>
<% operational = Role.find_by_name('operational').id %>
<%= f.input :role_id, :as => :label, :value => operational %>
<% end %>

Notice the change on :as => :label

It's just a suggestion, I will make a fork for me anyway and I think it's a good approach because altough the user can't change the input it's good practice that the user should see the data.

Greetings

Owner

justinfrench commented Nov 22, 2009

Ok, I think I better understand what you want now. You want the value to be editable by some people and not by others. For those that can't edit the data, we should display the value that would normally have been shown as the select option. Correct?

Owner

justinfrench commented Nov 23, 2009

(the original poster confirmed this via a direct message)

Owner

justinfrench commented Nov 23, 2009

My preferred method is to render as a :string with a disabled attribute and then use CSS to alter the appearance of the text field to look like plain text (remove the border, etc).

<%= f.input :ranking, :as => :string, :required => false, :input_html => { :disabled => true, :value => @post.ranking } %>

Closing this.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment