Multiple Views within subfolders

Hi,

I am creating an application in which I am allowing users to have customized views of a form. These forms do not have to be customized at this point by the user, but will for now be customized by us, and uploaded to the server.

My idea was to take the similar approach done by the plug in acts_as_attachment, and, have a default layout stored in the normal views/controller_name folder, but then also have a check in the users model to see if there is a customized view. If there was, the idea is to place a subfolder under the views/controller_name folder, with the id number of the user as the name of the folder, and place the customized partial within and then have the software display that partial, instead of the default one.

Im not sure if this is the best way of accomplishing this however, and would welcome any suggestions. I have yet to find a plug in to accomplish this.

If I do go this route, i cannot figure out how to have my main view call a partial that is stored in a sub-folder. Is there a way of doing this?

Thanks for you help/suggestions.

If I do go this route, i cannot figure out how to have my main view call a partial that is stored in a sub-folder. Is there a way of doing this?

render :partial => "subfolder/partial"

which you could enhance like that

render :partial => "#{@user.id}/partial"

would do the trick

i'm not sure if i would go that way, maybe storing the users html in the database and inserting in the page would be better, you could keep everything on a code level without having to create per-user directories in your view section (may depend on how often you have to add users, and of course you could write code to create a folder easy enough...) maybe the public folder would be a better place to store them if you have to...

Thanks for the reply!

Im am leaning towards storing the partials in the database. I agree that im may begin to be a nightmare storing them in the file system, and trying to keep everything in sync.

I have attempted to put this into the database, using the :text option in my migration. What is the proper way to render this though? If I go with <%= @member.name_of_partial %> it does not render the code, and just places the text in the browser window.

Thanks again for your help!

ok, so it's a 'real' partial then with <% ... %> stuff? then you would have to run the thing manually through erb

this should give you a start, it's less complicated, then it may appear on first view and very handy in other situations, too

http://www.ruby-doc.org/core/classes/ERB.html

hope that helps, otherwise post whatever questions you may have about it

Hi Thorsten,

That looks like it will work, I am just having issues passing an instance variable to the partial.

In my view I have the following:

  <% message = ERB.new(@member.company_information) %>   <%= message.run(@application.get_binding)%>

@member.company_information is where the partial stored in the database. @application is the instance variable that have to be passed to the company_information partial.

In the application model I have the following method:

def get_binding     binding end

The partial gets loaded, but the instance variables are not passed to it. Am I right to be calling the get_binding from the Application model?

Thanks!

first i'm not 100% sure, if run is the right command here or if you should use result (easy enough, to try both)

the binding should not be that of application, as far as i can make out from this question. (is application an ActiveRecord model?) as you use it, i think it would only bind to the class level instance variables defined in Application

somewhere you use (controller?) @application = Application.new(...) or @application = Application.find(...)

and that's the place, from where you need the binding (since that's the binding, that contains the @application)

so most likely in the controller: @bind = binding @application = Application.new(...) # or whatever

and in the view: <%= message.run(@bind)%>

hope that would do the trick, otherwise check step by step:

define a simple var (all in the view) <% @test = "test" %> <% message = ERB.new(@member.company_information) %> <-- this should use @test <%= message.run(binding)%> <-- just the binding of where we are

this should work in any case

Yep, that worked using result.

Some of the commands within the view, however, are not working. Is there something else that I have to pass to the ERB to allow for methods such as fields_for to work?

Here is a sample of what is in my view.

  <% for email_address in @application.applicant.email_addresses %>     <% fields_for "credit_application[applicant_attributes] [email_address_attributes]", email_address do |email_form| %>

      <%= email_form.hidden_field :name %>

      <label for="Applicant_Company_Legal_Name">Email Address:</label>       <%= email_form.text_field :email %><br/>

      <label for="Applicant_Company_Legal_Name">Confirm Email Address:</

      <%= email_form.text_field :email_confirmation %><br/>     <% end %>   <% end %>

This gives me the following errors:

undefined method `fields_for' for

The form_for tag is outside of this partial, but when i put it into it, I get the same error.

Is this because these are rails methods, and are not accessible from the ERB?

No Problem,

Thanks for all of your help. Ill let you know if I find a way to get it working.

stupid me :wink: of course it's rails and so it's easy!

render :inline => "<%= 'hello, ' * 3 + 'again' %>"

should do the trick

http://api.rubyonrails.org/classes/ActionController/Base.html#M000452

That did it.

Thanks again!

I have one more quick question.

I am attempting to place these partials into my migrations to have them loaded into the database.

I have on partial that looks like the following:

<% @application.references.each_with_index do |reference, reference_ix| -%>

      <h2>Reference</h2>       <label for="Reference_name">Search For Reference:</label>

      <%= my_text_field_with_auto_complete(:reference, :name, {:index => "#{reference_ix}", :size => 30 },                         :after_update_element => "function(element,value){"+                         remote_function(:url => {:action => :_reference_details, :reference_ix => reference_ix},                         :update => "ref_#{reference_ix}", :with => "'name='+element.value") + "}") %> <br/>

      <div id="ref_<%= reference_ix %>">

        ...

      </div>

      <% end -%>

How can I properly escape all of the double quotes and single quotes as well as the variable injection (I think that is what it is called -- "#{reference_ix}"). I have attempted delimiting the string using another character buy using %$ ...... $ but then it tries to interpret the variable injection. Is there a function that can be used to easily do this?

Thank for your help!