Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
wwb
Aug 17, 2004

We use redmine for lots of stuff here at the office. And we upgraded to redmine 2.0 recently which uses erb for it's views. Unfortuantely, it seems that the ActionMailer views are getting html escaped for plain text and we went plain text on the emails to kill ingest issues.

So, I've figured out enough to know I can go in and edit the .text.erb files in the mailer's views folder. But I was wondering if there was a more elegant approach available. I know very little ruby but I can pick up languages fast and I've got alot of experience with other mvc platforms so you can speak fast and use big words.

Adbot
ADBOT LOVES YOU

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.

A MIRACLE posted:

It's what happens when you decide to outsource development to a team of Eastern Europeans who don't speak English (our image gallery is spelled "Galery.rb") or care about things like unit test coverage, standards or best practices, and think it's okay to write 500 character SQL queries on a single LOC.

This is for a fairly prominent "daily deals" type site by the way, and the codebase is responsible for processing upwards of 200k in sales / day.

I was brought on last month because the higher-ups were all "oh poo poo we don't have any developers stateside." I'm basically the SQL monkey / put out fires ops guy right now but I'm pushing for a refactor / unit test project before any new features are added. We'll see. At least the pay is good for my area.

Oh man, you should see the SQL query we inherited (from a project originally developed by another company). It's 14 A4 pages long and is framed on the walls of our office. The project also comes with a 3 kLOC method that generates the query.

Deus Rex
Mar 5, 2005

A MIRACLE posted:

It's what happens when you decide to outsource development to a team of Eastern Europeans who don't speak English (our image gallery is spelled "Galery.rb") or care about things like unit test coverage, standards or best practices, and think it's okay to write 500 character SQL queries on a single LOC.

This is for a fairly prominent "daily deals" type site by the way, and the codebase is responsible for processing upwards of 200k in sales / day.

I was brought on last month because the higher-ups were all "oh poo poo we don't have any developers stateside." I'm basically the SQL monkey / put out fires ops guy right now but I'm pushing for a refactor / unit test project before any new features are added. We'll see. At least the pay is good for my area.

FWIW all of these horrible things (can't spell, insanely long lines, no thought about best practices, 0 unit tests) and more are at my workplace in the united states. it has nothing to do with them being ESL eastern europeans (who write some pretty good code for us)

wwb
Aug 17, 2004

So, turns out my issue was fixed in the next version which we aren't on yet. Doh. Disregard .. .

PS: speaking of lovely devs, we've now got a dedicated "Small Business Designer Develper" running our web shop who thinks valid database update procedures include mysql dump and valid editors include MySql Workbench. And he looks at me like I killed his kitten when I insist on source control and CI setups.

wwb fucked around with this message at 01:24 on Oct 18, 2012

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.
^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

Lexicon
Jul 29, 2003

I had a beer with Stephen Harper once and now I like him.

Lexicon posted:

^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

I've just inherited a rails app and for some reason the browser history is totally broken. You can navigate around the site just fine, but the URL in the URL bar never changes to anything other than the site root. I'm not really a javascript guy, but I'm assuming there's some javascript fuckery going on here. Can anyone point me in the right direction for sorting this out?

prom candy
Dec 16, 2005

Only I may dance
Is it a single page javascript app? Is it in a frame? Post some code probably.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Lexicon posted:

^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

I basically never have to touch the SQL (or other direct database interaction) of a Rails application working normally, including during schema changes. Using the Rails console and proper abstractions not only allows me to write tests that validate my changes, but also keeps me from doing a stupid UPDATE or DELETE command on the production database.

Is it a bad tool? Who cares! It's the wrong tool.

Computer Serf
May 14, 2005
Buglord
I'm considering learning ruby/rails via running this forum app in a live environment with a couple dozen users. Modifying and upgrading features over time. What could go wrong!? :haw:


Symposium posted:

Hobo is located at https://www.hobocentral.net - might want to edit your link.


  • Symposium Live Demo: http://agathon.herokuapp.com/
  • The tools I'm using

  • Heroku (Cedar stack) for hosting. I pine for a hosting solution for my vBulletin forum where I could feed it more resources with a slider bar and watch the impact in New Relic. I'm no database admin, so I've been logging countless nights into trying to figure out why the community's server would die on me.
  • Sorcery for authentication (logging in, logging out, current_user). It's much lighter and flexible than Devise. (github) (railscast)
  • CanCan for authorization (the admin role can create forums, the member role can only edit their own topics, etc.). (github) (railscast)
  • Twitter Bootstrap as a CSS framework. You'll notice that Symposium is clean and pretty in its live demo. I find development much more enjoyable when the application already looks alright from the first commit. It helps me think in turns of user experience as I code. It's also getting a responsive design in version 2.0. (website)

  • bb-ruby for post formatting. It renders bbcode from @post.body into HTML in @post.rendered_body. bb-ruby is to bbcode as RDiscount is to Markdown.



  • The implementation

    As you would imagine:

  • Models: User, Forum, Topic, Post, Ability (for CanCan)
  • User Roles: Admin, Member, Banned, Guest
  • Associations
  • Forums have many Topics
  • Topics have many Posts
  • Users have many Topics and Posts (as creator, so @topic.creator, @post.creator)

    Nesting:

    # routing
    resources :forums do
    resources :topics
    end
    resources :topics do
    resources :posts
    end



...any advice / comments / insults ?

edit: Github: https://github.com/danneu/symposium

Computer Serf fucked around with this message at 07:59 on Oct 19, 2012

Oh My Science
Dec 29, 2008
Why learn with Hobo? I remember running into it when I first started as well, but I decided to learn RoR with just RoR, no fancy additions. Does Hobo offer something I am not aware of? Is it used in a lot of production apps (their gallery is pretty sparse)?

Computer Serf
May 14, 2005
Buglord

Oh My Science posted:

Why learn with Hobo?

Sorry, I forgot to include the Github project link that includes all the Hobo modules into the "Symposium" forum app

wwb
Aug 17, 2004

Lexicon posted:

^^ Is MySql workbench really that bad? I've never used it, but assumed it was the de facto standard tool.

MySql workbench is a great MySql management and troubleshooting tool. It is not meant to be your CMS admin tool in this developer's opinion. No that we'd never let the designers closer than a reviewed sql script to the actual production database.

When I mentioned rails to the guy he got scared. Little too much formality it seems.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Can anyone give me some tips on speeding up Rails 3 apps in development mode?

We have Ruby 1.87 and while our Rails 2 apps are fine, I literally wait 20-40 seconds to re-load a page in development mode on my machine. The apps run fine our production server which is a 2.26GHz Xeon on RHEL 5 (L5520), but on either development machine I use, they're mind-numbingly slow. One is a Mac with 3.06GHz i3, 8GB, SSD, the other is a C2Q with 6GB running CentOS 6.3

The development DB is a MySQL server on the LAN and it seems pretty fast, it's just the rendering. If I do a ColorBox and put 'hello world' in it, it's like it takes 10 seconds to fill the box. I just installed thin and am trying that instead of Webbrick and it's not noticeably faster.

prom candy
Dec 16, 2005

Only I may dance
Have you tried https://github.com/thedarkone/rails-dev-boost

If that doesn't work you'll probably need to profile your requests somehow to figure out where the slowdowns are happening. Does it happen if you don't use the asset pipeline?

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

prom candy posted:

Have you tried https://github.com/thedarkone/rails-dev-boost

If that doesn't work you'll probably need to profile your requests somehow to figure out where the slowdowns are happening. Does it happen if you don't use the asset pipeline?

If he's on Rails 2 he doesn't have the asset pipeline, which only came about after Rails 3.1.

That plugin looks interesting. It reminds me of how Scribd handled development mode (they have a huge Rails 2 app). They basically ran a watch script (think SASS compiler) that monitored and reloaded classes only when they were modified, instead of dumping the whole app and reloading every class per request.

It's an interesting problem.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

prom candy posted:

Have you tried https://github.com/thedarkone/rails-dev-boost

If that doesn't work you'll probably need to profile your requests somehow to figure out where the slowdowns are happening. Does it happen if you don't use the asset pipeline?

Wow, that made a huge improvement. I guess I won't be drinking before lunch.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Bob Morales posted:

Can anyone give me some tips on speeding up Rails 3 apps in development mode?

We have Ruby 1.87 and while our Rails 2 apps are fine, I literally wait 20-40 seconds to re-load a page in development mode on my machine. The apps run fine our production server which is a 2.26GHz Xeon on RHEL 5 (L5520), but on either development machine I use, they're mind-numbingly slow. One is a Mac with 3.06GHz i3, 8GB, SSD, the other is a C2Q with 6GB running CentOS 6.3

The development DB is a MySQL server on the LAN and it seems pretty fast, it's just the rendering. If I do a ColorBox and put 'hello world' in it, it's like it takes 10 seconds to fill the box. I just installed thin and am trying that instead of Webbrick and it's not noticeably faster.

It's in development mode, so it's re-loading everything on every request. How many models and other classes do you have?

Also, it's in development mode, nobody but you is gonna complain if you profile it and find out where the speed problems are.

Big Nubbins
Jun 1, 2004
I think I did a bad thing. This morning, mysteriously my rails server wouldn't start. It was bombing out with the message /Users/ShameBoner/.rvm/gems/ruby-1.9.3-p286@rails_3_2/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- trace_nums (LoadError). I suspect that I properly hosed my gemset by doing a bundle update while rubygems.org's dependency API is down. I'm not sure what gem is responsible for the TraceNums class, but it seems to be missing. Is there any way to un-gently caress this gemset? For the time being, I had to go back to using my 1.9.2 version of Ruby.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

Shame Boner posted:

I think I did a bad thing. This morning, mysteriously my rails server wouldn't start. It was bombing out with the message /Users/ShameBoner/.rvm/gems/ruby-1.9.3-p286@rails_3_2/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- trace_nums (LoadError). I suspect that I properly hosed my gemset by doing a bundle update while rubygems.org's dependency API is down. I'm not sure what gem is responsible for the TraceNums class, but it seems to be missing. Is there any way to un-gently caress this gemset? For the time being, I had to go back to using my 1.9.2 version of Ruby.

That's always fun. I hope this isn't for a big project because it could get a little hairy. If your Gemfile.lock is in version control you can look at a previous version where everything was working, then manually go through your gemfile, setting specific versions of all your gems. From there just update as necessary.

Pardot
Jul 25, 2001




Shame Boner posted:

I think I did a bad thing. This morning, mysteriously my rails server wouldn't start. It was bombing out with the message /Users/ShameBoner/.rvm/gems/ruby-1.9.3-p286@rails_3_2/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- trace_nums (LoadError). I suspect that I properly hosed my gemset by doing a bundle update while rubygems.org's dependency API is down. I'm not sure what gem is responsible for the TraceNums class, but it seems to be missing. Is there any way to un-gently caress this gemset? For the time being, I had to go back to using my 1.9.2 version of Ruby.

You can try adding source "http://bundler-api.herokuapp.com" to your gemfile. It is another api endpoint that is up to date as of last night. It is just a proof of concept though, and isn't like a real real thing, but could get you going.

dexter
Jun 24, 2003

Bob Morales posted:

Can anyone give me some tips on speeding up Rails 3 apps in development mode?

We have Ruby 1.87 and while our Rails 2 apps are fine, I literally wait 20-40 seconds to re-load a page in development mode on my machine. The apps run fine our production server which is a 2.26GHz Xeon on RHEL 5 (L5520), but on either development machine I use, they're mind-numbingly slow. One is a Mac with 3.06GHz i3, 8GB, SSD, the other is a C2Q with 6GB running CentOS 6.3

The development DB is a MySQL server on the LAN and it seems pretty fast, it's just the rendering. If I do a ColorBox and put 'hello world' in it, it's like it takes 10 seconds to fill the box. I just installed thin and am trying that instead of Webbrick and it's not noticeably faster.

Are you using a local redis or memcached? Sometimes a reference to localhost will be resolved as its v6 IP (::1) and those normally only bind to 127.0.0.1. Get off Ruby 1.8.7, just upgrading to 1.9.3 will get you some decent gains.

mmachine
Jan 5, 2006
I'm trying to understand how to implement click-to-edit functionality. I've seen some gems suggested for doing this, but since the purpose of the project I'm working now is to teach myself how to use RoR, I'd prefer to at least learn an implementation from scratch.

What I have so far is a page -- let's call it Edit Recipe -- that then allows a user to add and remove both Ingredients and Directions / Steps. I've been able to get far enough as to implemnent this functionality using AJAX, but now I've hit a wall when trying to implement a way to edit pre-existing data for each item. Say a user has entered a recipe direction -- I'd like to give them the ability to click on an edit call to action and then be able to change the content of the direction, submit and have it be saved.

Where I'm failing now is in getting the form partials -- let's use the _form for Directions as an example -- to render in the context of an Edit. So, say I have 4 directions listed, I do get a form rendered for each, though the form is for a create new direction, not an update existing.

So now, what I need to understand is what should be happpening to get those Direction forms to understand they should be appearing with the data for their respective record when they are placed into the DOM, AND also understand that on post, they are to edit their respective Direction record. The way the forms currently get placed is via a line of JavaScript that goes like this:


$('#direction_<%= direction.id %> .edit').click(function(){
$('#direction_<%= direction.id %>').append("<%= escape_javascript(render :partial => 'directions/form', :locals => { :direction => direction }) %>");
});
...and then the partial for my Direction _form (currently) looks like this:



<% if defined? direction %>

<%= form_for :direction, :url => { :action => 'edit', :id => direction.id }, :remote => true do |f| %>
<div class="input-append">
<%= f.text_field :content, :class => 'input-xlarge' %>
<%= f.submit :value => 'save', :class => 'btn' %>
</div>
<% end %>

<% else %>

<%= form_for([@recipe, @recipe.directions.build], :remote => true) do |f| %>

<div class="input-append">
<%= f.text_field :content, :class => 'input-xlarge' %>
<%= f.submit :value => 'add', :class => 'btn' %>
</div>

<% end %>

<% end %>


...where I'm trying to at least pass the Direction object along, thinking that's all the partial needs to render as an edit form. Of course now I have the problem of this not working, the form still thinking it's posting a new direction, and not editing a pre-existing one, so I'm missing something big on defining the controller resources to use for the form maybe? I was under the impression the partials would be conscious of a Direction object, and just do their thing automagically.

Lots I'm doing wrong practice wise too maybe -- for example, the contextual partial for the Direction _form seems awkward, but maybe that's the way to do this sort of thing?

EDIT: Sorry for the ugly formatting. Copypasta skills need some work too it looks like...

mmachine fucked around with this message at 15:50 on Oct 22, 2012

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.


Wow, that seems kind of awkward, I didn't know people still inlined ruby in their javascript code. I hate to recommend js frameworks since it seems like you're already really far along with this method, but I've been really happy using either Angular, Backbone, or Knockout.js (with underscore.js as a utility belt) for my ajax-y goodness.

mmachine
Jan 5, 2006

A MIRACLE posted:

Wow, that seems kind of awkward, I didn't know people still inlined ruby in their javascript code. I hate to recommend js frameworks since it seems like you're already really far along with this method, but I've been really happy using either Angular, Backbone, or Knockout.js (with underscore.js as a utility belt) for my ajax-y goodness.

Ha! Well it definitely felt awkward.

I'm not opposed to using a JS framework, though I'm wondering if understanding how this works without the framework in place yet will get me a better handle on how these things talk together.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

mmachine posted:

Ha! Well it definitely felt awkward.

I'm not opposed to using a JS framework, though I'm wondering if understanding how this works without the framework in place yet will get me a better handle on how these things talk together.
Without the framework, it feels like a huge obtrusive hack. Send the form in a script when the page first loads, and use JavaScript to activate it when the click to edit field is clicked on.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

mmachine posted:

Ha! Well it definitely felt awkward.

I'm not opposed to using a JS framework, though I'm wondering if understanding how this works without the framework in place yet will get me a better handle on how these things talk together.

Well the way I use them is, I basically treat Rails like an API / REST layer with routing and business logic, and then the js on the front just talks to Rails.

I think (Backbone || Angular) + underscore would be a good starting point if you're actually interested in using a js framework.

I've used Knockout extensively but that was with .NET, not sure how well it gels with Rails.

wwb
Aug 17, 2004

Knockout itself is quite platform independent, but it comes from a very MS background (MVVM on Silverlight and WPF apps) so it tends to get tied up in MS web technologies. But I don't see any reason that you couldn't use it with rails presuming you got your rails setup right.

mmachine
Jan 5, 2006

Cocoa Crispies posted:

Without the framework, it feels like a huge obtrusive hack. Send the form in a script when the page first loads, and use JavaScript to activate it when the click to edit field is clicked on.

Never even thought about it this way, but this makes a lot of sense. Sounds like you're saying by the time I figure out to get this all working via RoR my long way, I'll just want to start tying in a framework to alleviate tedium. Sound about right?

As far as JS frameworks go, I've liked what I've seen of Angular so I think I'll start there.

tima
Mar 1, 2001

No longer a newbie

mmachine posted:

I'm trying to understand how to implement click-to-edit functionality. I've seen some gems suggested for doing this, but since the purpose of the project I'm working now is to teach myself how to use RoR, I'd prefer to at least learn an implementation from scratch.

What I have so far is a page -- let's call it Edit Recipe -- that then allows a user to add and remove both Ingredients and Directions / Steps. I've been able to get far enough as to implemnent this functionality using AJAX, but now I've hit a wall when trying to implement a way to edit pre-existing data for each item. Say a user has entered a recipe direction -- I'd like to give them the ability to click on an edit call to action and then be able to change the content of the direction, submit and have it be saved.

Where I'm failing now is in getting the form partials -- let's use the _form for Directions as an example -- to render in the context of an Edit. So, say I have 4 directions listed, I do get a form rendered for each, though the form is for a create new direction, not an update existing.

So now, what I need to understand is what should be happpening to get those Direction forms to understand they should be appearing with the data for their respective record when they are placed into the DOM, AND also understand that on post, they are to edit their respective Direction record. The way the forms currently get placed is via a line of JavaScript that goes like this:


$('#direction_<%= direction.id %> .edit').click(function(){
$('#direction_<%= direction.id %>').append("<%= escape_javascript(render :partial => 'directions/form', :locals => { :direction => direction }) %>");
});
...and then the partial for my Direction _form (currently) looks like this:



<% if defined? direction %>

<%= form_for :direction, :url => { :action => 'edit', :id => direction.id }, :remote => true do |f| %>
<div class="input-append">
<%= f.text_field :content, :class => 'input-xlarge' %>
<%= f.submit :value => 'save', :class => 'btn' %>
</div>
<% end %>

<% else %>

<%= form_for([@recipe, @recipe.directions.build], :remote => true) do |f| %>

<div class="input-append">
<%= f.text_field :content, :class => 'input-xlarge' %>
<%= f.submit :value => 'add', :class => 'btn' %>
</div>

<% end %>

<% end %>


...where I'm trying to at least pass the Direction object along, thinking that's all the partial needs to render as an edit form. Of course now I have the problem of this not working, the form still thinking it's posting a new direction, and not editing a pre-existing one, so I'm missing something big on defining the controller resources to use for the form maybe? I was under the impression the partials would be conscious of a Direction object, and just do their thing automagically.

Lots I'm doing wrong practice wise too maybe -- for example, the contextual partial for the Direction _form seems awkward, but maybe that's the way to do this sort of thing?

EDIT: Sorry for the ugly formatting. Copypasta skills need some work too it looks like...

Have you looked at the railscasts cast on this topic? http://railscasts.com/episodes/136-jquery-ajax-revised

Physical
Sep 26, 2007

by T. Finninho
Here's what I think is turning out to be a crazy question: how do I embed ruby tags in a string? For example I want to do this

something.html.erb
Ruby code:
  <%
    _a = %q{
      <div class="control-group">
        <%= f.label :field_name, t('field_name'), :class => 'control-label' %>
      </div>}     
  %>
  <%= _a %>
Although now that I am looking at it, the closing %> on the label tag is probably not helping this at all

Physical fucked around with this message at 14:21 on Oct 24, 2012

Civil Twilight
Apr 2, 2011

Physical posted:

Here's what I think is turning out to be a crazy question: how do I embed ruby tags in a string?

What are you %q{}ing for anyway? Is there something important you're not telling us?

Ruby code:
      <div class="control-group">
        <%= f.label :field_name, t('field_name'), :class => 'control-label' %>
      </div>

Physical
Sep 26, 2007

by T. Finninho
Yea I want to store these fields in an array so I can rearrange them later. I'd rather not use partials for this.

So what you see me doing is trying to store an input field in a string so I can put that in an array, then output them later at will.

e: It's in an html.erb btw.

ee: Here is what worked:
Ruby code:
  <%
    _a =
<<-eoss
          <div class="control-group">
            #{f.label :field_name, t('field_name'), :class => 'control-label'}
            <div class="controls">
               #{select_tag :field_name, options_for_select(["High", "Medium", "Low"], @item.field_name), :prompt =>"Please Pick a Field Name", :name => "item[field_name]", :selected => @item.field_name}
            </div>            
          </div>
eoss%>
  <%= _a.html_safe %>  
I dropped the <%= %> and used #{} instead. Still would rather be able to do it with the <%= %> inside of a larger <% %> tag but that's unpossible :downs:

Physical fucked around with this message at 15:31 on Oct 24, 2012

prom candy
Dec 16, 2005

Only I may dance
You might want to use a helper or something for this, you're going to punch yourself in the face when you have to edit that code in six months.

Physical
Sep 26, 2007

by T. Finninho

prom candy posted:

You might want to use a helper or something for this, you're going to punch yourself in the face when you have to edit that code in six months.
What would the helper do? I intended to make some helpers since this was going to be used more than once and dynamic, but I'd like to know what you would suggest?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Physical posted:

What would the helper do? I intended to make some helpers since this was going to be used more than once and dynamic, but I'd like to know what you would suggest?
The helper would do the string templating. http://railspikes.com/2008/8/22/how-to-fix-your-rails-helpers

Siguy
Sep 15, 2010

10.0 10.0 10.0 10.0 10.0
A few questions for you, Physical:

Why do you want to put it in an array?
Why don't you want it in a partial?

Or more generally: What's the view functionality you're trying to achieve?

I'm sure that what you want to do is possible (you already have a working version it seems), but this method definitely seems odd and makes me think you've crafted an elaborate solution to something that may already have a simpler, Rails-y solution.

In particular I'm wondering if your view is going to involve some big crazy array that has all the html for your page and ends up looking like:

code:
<html>
<body>
	<%= big_crazy_array[4] %>
	<%= big_crazy_array[85] %>
	<%= big_crazy_array[9] %>
	<%= big_crazy_array[0] %>
	Copyright Physical 2012
</body>
</html>

Physical
Sep 26, 2007

by T. Finninho

Siguy posted:

A few questions for you, Physical:

Why do you want to put it in an array?
Why don't you want it in a partial?

Or more generally: What's the view functionality you're trying to achieve?

I'm sure that what you want to do is possible (you already have a working version it seems), but this method definitely seems odd and makes me think you've crafted an elaborate solution to something that may already have a simpler, Rails-y solution.

In particular I'm wondering if your view is going to involve some big crazy array that has all the html for your page and ends up looking like:

code:
<html>
<body>
	<%= big_crazy_array[4] %>
	<%= big_crazy_array[85] %>
	<%= big_crazy_array[9] %>
	<%= big_crazy_array[0] %>
	Copyright Physical 2012
</body>
</html>

Physical posted:

So what you see me doing is trying to store an input field in a string so I can put that in an array, then output them later at will.
We are giving the user the option to control in which order their input fields are displayed on a form. So the array will have like 9 indices with as much code in them as I put above, about 4 or 5 lines.

Too little for a partial I think because then I would have 9 partials. Then we are going to do this site wide with our other forms. I would love to have a more rails-y way to do this.

prom candy
Dec 16, 2005

Only I may dance
Generally if you find yourself doing variable assignment in your ERB you're doing something wrong.

Here's a really simple example of how to convert what you're doing to helper code, but you can probably clean it up a little better.


code:
module FormHelper
  def group(f)
    content_tag :div, :class => 'control-group' do
      f.label(:field_name, t('field_name'), :class => 'control-label) +
      content_tag :div, :class => 'controls' do
        select_tag(:field_name, options_for_select(["High", "Medium", "Low"], @item.field_name), :prompt =>"Please Pick a Field Name", :name => "item[field_name]", :selected => @item.field_name)
      end
    end
  end
end

# In your erb

<%= form_for @item do |f| %>
  <%= group(f) %>
<% end %>

Edit: Just saw your reply above. No such thing as "too little for a partial."

Physical
Sep 26, 2007

by T. Finninho

prom candy posted:

Generally if you find yourself doing variable assignment in your ERB you're doing something wrong.

Here's a really simple example of how to convert what you're doing to helper code, but you can probably clean it up a little better.


code:
module FormHelper
  def group(f)
    content_tag :div, :class => 'control-group' do
      f.label(:field_name, t('field_name'), :class => 'control-label) +
      content_tag :div, :class => 'controls' do
        select_tag(:field_name, options_for_select(["High", "Medium", "Low"], @item.field_name), :prompt =>"Please Pick a Field Name", :name => "item[field_name]", :selected => @item.field_name)
      end
    end
  end
end

# In your erb

<%= form_for @item do |f| %>
  <%= group(f) %>
<% end %>

This is where I was hoping to go, thanks. It makes the exercise in "storing stuff in a string" obsolete, although it was a more tangible way for me to get an understanding of how this thing could work.

I owe you more money now.

Adbot
ADBOT LOVES YOU

prom candy
Dec 16, 2005

Only I may dance
After seeing your other response I really would advise you to put these bits into partials rather than the helper. Partials are great for this kind of thing. I just built a survey-builder type system for an app I was working on and the surveys are all built out of partials, it keeps the code pretty clean.

code:
          - question_group.questions.each do |question|
            .question
              %h2
                = question.title
                = question.required
              = question.explanation_text
              .answer= render question.answer_partial, :f => f, :question => question
(This is HAML but you should be able to see what I mean)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply