Multiple layouts in rails [Newbie Q] -


As a newb, I decided to create a "home inventory" application. Now, while looking into a program, I want to programmatically select a layout based on which type of item.

According to my plan, so far I can find some models in my house: Furniture , Electronics and Books .

  class book & lt; ActiveRecord :: Base & Class Furniture & lt; ActiveRecord :: Base & Class Electronic & lt; ActiveRecord :: Base End  

Now in the Books model things like isbn , pages , address , and category . The Furniture model contains things like color , value , addresses , and category . names , voltage , address , and category such as electronics .

Here's where I was confused I know that property address will be identical to all of them, I also know that the appropriate To show the different qualities of objects that came with graphics and stylesheets, I would need to create several "layouts" for 3 different types of items.

But what category do I want to decide is the item so that I can render the layout.

According to me, how do I do this:

  class DisplayController & lt; ApplicationController def Display @item = Ultimate [: Item] If @ item.category = "Electronics" render: layout = & gt; 'Electronics' End End  

In My Routes Rb

  map.display ': item' ,: controller = & gt; 'Display',: action = & gt; 'Display'  

I just have a concern with it, I will probably add many categories later and it seems that there should be another drought-prone method to deal with it instead.

---- Question I understand that to display relevant information for that particular category, I need to add my layout to the HTML tags.

---- Question ----

  1. Is this the right way to deal with this kind of problem?
  2. Will this approach be consistent when I thinking_sphinx
  3. what problems do you face with my attitude and how can I improve it?
  4. I was reading something about "polymorphic asok", does this apply in this case because the range exists for all items?

In addition, I have a URL such as ""

If I understand you correctly, then what do you want is not a separate layout Do you want different templates?

Layout means the entire page's general structure, and it usually happens for the entire project, or you may have 2-3 layouts for each project. (Site wide layout, admin layout and some other specific layouts)

What you talk about is that you want a different template to show different characteristics of books, furniture and electronics.

P> A visual file app / view / display / furniture. To create HR and other books and electronics, and render it to : template = & gt; Instead of 'furniture' render: layout .

Now if there are common pieces of information in different categories, then what you should do is partially the only common part, for example, a partial app / view / share / address.html. Create ARB, and you will present inside your furniture.html.erb: partial = & gt; '/ Share / address' ,: local = & gt; {: Address = & gt; @ Item.address} .

This will help you keep visual logic in as dry form as possible, while providing flexibility to create custom templates for each category.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -