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 removi开发者_StackOverflowng read-only attributes from submit parameters?
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]
.
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.
Maybe you want to try token fields, it is well explained here http://railscasts.com/episodes/258-token-fields
精彩评论