nurettin
nurettin

Reputation: 11736

custom field in rails form

In my /app/views/institution/_form.html.erb I have

<%= f.textfield :auto_complete_list %>

Which gets its data from /app/models/institution.rb

  def auto_complete_list
    return self.county.city.name + ' '+ self.county.name
  end

But I don't want this to be submitted when the button is pressed. My current solution is to delete it from params[:institution] Is there a cleaner approach to removing read-only attributes from submit parameters?

Upvotes: 1

Views: 3985

Answers (3)

Jeremy Weathers
Jeremy Weathers

Reputation: 2554

While I agree with daekrist's answer, you can easily keep this out of params[:institution] by using the lower level form helper methods that:

<%= form_for @institution do |f| %>
  ...
  <%= text_field_tag 'auto_complete_list', @institution.auto_complete_list %>
  ...
<% end %>

So now when the form is submitted, it will be in params[:auto_complete_list] instead of params[:institution][:auto_complete_list].

Upvotes: 3

eveevans
eveevans

Reputation: 4460

Maybe you want to try token fields, it is well explained here http://railscasts.com/episodes/258-token-fields

Upvotes: 1

Jake Jones
Jake Jones

Reputation: 1170

I suppose, a cleaner approach will be to never put things like this in your forms :)

If you want autocomplete, it would be better to use some js/jquery plugin and attach it to the field you need.

Upvotes: 1

Related Questions