dry

Where to put partials shared by the whole application in Rails?

左心房为你撑大大i 提交于 2019-12-03 18:55:22
问题 Where would I go about placing partial files shared by more than one model? I have a page called crop.html.erb that is used for one model - Photo . Now I would like to use it for another model called User as well. I could copy and paste the code but that's not very DRY, so I figured I would move it into a partial. Since it's shared between two models - where would I place that partial ? Thanks! 回答1: The Rails convention is to put shared partials in /app/views/shared . 回答2: Update Layout

ASP.NET MVC - How to achieve reusable user controls and maintain DRY?

守給你的承諾、 提交于 2019-12-03 16:56:06
问题 First post so please be gentle :) When creating user controls in ASP.NET MVC, what is the best way to structure the code so that the controllers that invoke views that use the user controls do not all have to know so much about the controls? I would like to know a good way to maintain DRY while using user controls in ASP.NET MVC. Please note, this question only pertain to user controls that require special handling and logic on a postback. I have no problem creating nice DRY code for user

How can you be DRY with a programming language that doesn't have Reflection? [closed]

拥有回忆 提交于 2019-12-03 15:57:06
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago . Any programming language that does not have a suitable reflection mechanism I find seriously debilitating for rapidly changing problems. It seems with certain languages its incredible hard or not possible to do: Convention over

Can I inherit constructors?

。_饼干妹妹 提交于 2019-12-03 14:21:07
问题 I know it's not possible to inherit constructors in C#, but there's probably a way to do what I want to do. I have a base class that is inherited by many other classes, and it has an Init method that does some initializing taking 1 parameter. All other inheriting classes also need this initializing, but I'd need to create separate constructors for all of them that would like like this: public Constructor(Parameter p) { base.Init(p); } That totally violates the DRY principles! How can I have

Creating html templates using PHP

旧城冷巷雨未停 提交于 2019-12-03 13:58:01
Im learning a lot about how MVC frameworks work by looking around and studying existing ones. It seems that every framework I see has a layout where each method in each controller has its own template file. So there will be a login template, a logout template, register, so on and so on. My question is, how and why would you create a template for the entire page all within one file. Lets say you wanted to show the login form on more than one page, wouldn't you have to create the login form for each template that you want it to display on? Doesn't that go against the don't repeat yourself rule

How can I avoid repeating myself when creating a C++ enum and a dependent data structure? [duplicate]

假装没事ソ 提交于 2019-12-03 13:27:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Enum to string : return the enum integer value if invalid / not found In brief, the (working) definition code that I have is something like this: enum Gadget { First, Second, }; const char* gadget_debug_names[] = { "First", "Second", // note: strings are same as enum tokens here, but bonus points if // they can optionally be given different values }; However, it's error prone that the information is in multiple

RESTful grails application: DRYing up UrlMapping

邮差的信 提交于 2019-12-03 13:24:50
Let's say we have a grails web application exposing several resources. tags urls users The application has a classical web-interface which the users interact with and some administration. We want to expose the resources from the application to clients via a RESTful API and we don't want that part of the app to clutter up the controllers and code we already have. So we came up with the following: If the web interface offers host/app_path/url/[list|show|create] we want the REST API to be at /host/app_path/rest/url . So we ended up with the following UrlMappings file: class UrlMappings { static

DRY Controllers in Rails 3.2

99封情书 提交于 2019-12-03 12:43:49
Following code climate analysis, I found that my controllers are not DRY as it could be. The methods like: def index @animals = current_user.animals.valid_animals.search(params[:search], params[:page]) respond_to do |format| format.html # index.html.erb format.json { render json: @animals } end end Are basically equal in all the controllers. Basically, the scaffold rails generated code are "the same" in all controllers. How can I made it more clean and dry in a real good way? thanks in advance You could use respond_with for these actions. class AnimalController < ApplicationController respond

How to elegantly symbolize_keys for a 'nested' hash

别来无恙 提交于 2019-12-03 09:18:58
Consider the following code: hash1 = {"one" => 1, "two" => 2, "three" => 3} hash2 = hash1.reduce({}){ |h, (k,v)| h.merge(k => hash1) } hash3 = hash2.reduce({}){ |h, (k,v)| h.merge(k => hash2) } hash4 = hash3.reduce({}){ |h, (k,v)| h.merge(k => hash3) } hash4 is a 'nested' hash i.e. a hash with string keys and similarly 'nested' hash values. The 'symbolize_keys' method for Hash in Rails lets us easily convert the string keys to symbols. But I'm looking for an elegant way to convert all keys (primary keys plus keys of all hashes within hash4) to symbols. The point is to save myself from my ( imo

DRY vs Security and Maintainability with MVC and View Models

风流意气都作罢 提交于 2019-12-03 07:41:14
I like to strive for DRY, and obviously it's not always possible. However, I have to scratch my head over a concept that seems pretty common in MVC, that of the "View Model". The View Model is designed to only pass the minimum amount of information to the view, for both security, maintainability, and testing concerns. I get that. It makes sense. However, from a DRY perspective, a View Model is simply duplicating data you already have. The View Model may be temporary, and used only as a DTO, but you're basically maintaing two different versions of the same model which seems to violate the DRY