goo
goo

Reputation: 2280

Rails: Text field as regular text

I would just like to display a text field without making it editable. I know there's 'read only' or 'disabled', but is there a way for me to just display the text field as regular text?

<%= f.text_field :attr %>

Edit to clarify things:

<% form_for(my_form) do |f| %>
    <%= f.text_field :attr %>
    // How would I print my :attr as regular text?
<% end %>

Closed

I decided to stick with the input field and just use some CSS

What I was trying to do: I have 3 nested fields (a table called my_colors -> blue, red, green under my t.string :color), and for each "my_color", I have a t.integer :amount. Since those nested fields were pre-built, I didn't want the user to edit the color but I still wanted to show it without using readonly or disabled.

Upvotes: 0

Views: 53

Answers (3)

goo
goo

Reputation: 2280

I decided to stick with the input field and just use some CSS

What I was trying to do: I have 3 nested fields (a table called my_colors -> blue, red, green under my t.string :color), and for each "my_color", I have a t.integer :amount. Since those nested fields were pre-built, I didn't want the user to edit the color but I still wanted to show it without using readonly or disabled.

Upvotes: 0

RobinSH
RobinSH

Reputation: 131

Can you describe what you are attempting to do a bit more clearly? For example, you have a form there which implies creating a new object within your database. Usually a form in Rails is tied to the create action for your relevent controller.

If you just want to show the attributes of an item which has already been created, you can do something like this:

(My example uses a Post):

app/views/posts/show.html.erb

<p> This post's attributes are: <%= @post.attr %></p>
<p> This post's id is: <%= @post.id %></p>

And then in your posts_controller.rb:

def show
  @post = Post.find(params[:id]
end

Upvotes: 1

a-b-r-o-w-n
a-b-r-o-w-n

Reputation: 515

A <p></p> is what you need.

Upvotes: 0

Related Questions