I am using Ruby on Rails 3.2.2. In order to implement a “dynamic generated” AJAX style file upload form I would like to “dynamically add options” to the FormHelper#form_for statement if some conditions are meet. That is, at this time I am using code as-like the following (note that I am using the merge method in order to add options to the form_for method):
<%
if @article.is_true? && (@article.is_black? || && @article.is_new?)
form_options = {:multipart => true, :target => "from_target_name"}
else
form_options = {}
end
%>
<%= form_for(@article, :remote => true, :html => {:id => "form_css_id"}.merge(form_options)) do |form| %>
...
<% end %>
However, I think that the above code is too much hijacked.
Is there a better way to accomplish what I am making? For example, can I access from view templates some (unknown to me) instance variable named as-like @form and “work” on that so to change related options as well as I would like? Or, should I state a helper method somewhere? How do you advice to proceed?
BTW: Since the upload process is handled by using a HTML iframe, I am using the remotipart gem in order to implement the AJAX style file upload form – I don’t know if this information could help someone…
This looks like a good candidate for a helper method. In your view:
In
app/helpers/articles_helper.rbHelpers are a good place to keep logic that’s too complex for a view but still related to the view.