ruby on rails - conditional view/layout -


Say that there is a product controller in which you want to do an index (list product) action easier. Now say you have an admin and store parts in your project. Both need a list of products, but in a slightly different way (should not edit product links like this in store). They also use different layouts

So far my idea is to have two product controllers under different namespace. app / controllers / admin / products_controller.rb and application / controllers /store/products_controller.rb - after all it is their own idea and layout. But I suspect that this could be a WET code. Or reference to other controller ideas (which imo modularity is broken and therefore should be avoided)

Then, the real question:? There is one more dry (or really reasonable) way to get up

I'm not sure that the title actually reflects the question. But, on the other hand, if it is, maybe I can answer the answer.

As the edit 3.1, the rail template supports succession.

If you are displaying the product between the admin section and stores section, Admin link (create, Edit, delete), then I think it would be easiest to partial your product. I think that you have a way of saying that the user is an administrator or not (I'll just use the admin for simplicity below). Within your partial you do something like this ...

  & lt; Div class = "product" & gt; & Lt; Div class = "productholder" & gt; & Lt;% = h product.title% & gt; & Lt; / Div & gt; & Lt; div class = "productdescription & gt; & lt;% = h product.description% & gt; & lt; / div & gt; & lt;% if the administrator% & gt ;? & Lt; div class =" productadmin "& gt; & lt;% = LINK_TO "Delete", Nsht_prodkt_uarel% & gt; & lt;% = link_to "edit", edit_product_url% & gt; & lt; / div & gt; & lt;% end% & gt;  

Name this partial _product.html.erb (the underscore rail that the template is a partial). Applications of your application / ideas folder in the directory Create a shared call and partial shop there.

Your other views to render In this case, call partial, only render method and pass partial parameter.

Single Product:

  <% = render (: partial = & gt; "share / product" ,: object = & gt; @a_product)% & gt;  

multiple products:

  & lt;% = render (: partial = & Gt; "share / product", collection = & gt; @ products)% & gt;  

the layout can be implemented by adding digital out parameters Partial layout should be prefixed with underscore, but not limited to Related Ntrk app / views should be stored in the directory.

  & lt;% = Render (: partial = & gt; "Share / Product" ,: Object => @A_product, layout = & gt; "somelayout"% & gt;  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -