Sunday 15 July 2012

ruby on rails - Actions on an instance object passed to the view -


I have an obj @ files which are sometimes a set of data (a file) and sometimes in There are many files

I have create.js.erb and what do I do at this time.

  & lt;% if @ files.new_record? & Gt%; Warning ("Failed to upload: & lt;% = j @ files.errors.full_messages.join (','). HTML_safe% & gt;"); & Lt;% else% & gt; $ (". Container") .andend ('  gt; & lt; / div & gt;'); & Lt;% end% & gt;   

Then I have a partial _file.html. ARB

  and <% = file.name%>. & Lt;% = file_.id% & gt;   

All this works well, but I am trying to partially create different types of files.

I want to be able to do something like this

  if file.first.type == image # (Even if they come in groups then they There will be only types, so a type of field is required from one of them.) $ ("Different containers"). Attachments (separate partially presented); Otherwise if file.first.type == doc $ (".other_container"). Attachment (partially presented here);   

How would I go about this? Please ask me clearly I have not understood anything.

The problem is that @ files can be multiple items or single items before you access it Do this, you can wrap it in array () :

  file_type = Array (@files) .first.type if file_type ==: some elsif File_type ==: some_else end   

what array () does this tries to convert that argument into an array. If you pass an object to it, then an array with that object will return. If you pass an array to it, then it does nothing.

  & gt; & Gt; Array (1) # = & gt; [1] & gt; & Gt; Array (object.New) # = & gt; [Object] & gt; & Gt; Array ([1,2,3]) # = & gt; [1,2,3]    

No comments:

Post a Comment