Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:52:49+00:00 2026-06-08T13:52:49+00:00

What is a better (proper) way to code this according to MVC? Here’s the

  • 0

What is a better (proper) way to code this according to MVC? Here’s the situation:

I have a collection_select in my form, and it works fine, however I’m wanting to do something like this (note the text of the option, the 4th arg)

#Form.html.haml
= form_for(@payment) do |f|
 = f.collection_select(:invoice_id, Invoice.due, :id, :number + ' ' + money(:amount_due))

That won’t work because as far as I know, you can’t concat the symbols. So, I resorted to this:

#Form.html.haml
= form_for(@payment) do |f|
  = f.collection_select(:invoice_id, Invoice.due, :id, :number_with_amount_due)

#Invoice.rb
def number_with_amount_due
  "##{number} (#{money(amount_due)})"
end

Which would work if I wasn’t trying to call a helper method in the Model.

So what should I do? It would be nice to be able to concat the select text in the views, but if not possible, how can I achieve what I’m trying to do while retaining proper MVC in Rails?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:52:53+00:00Added an answer on June 8, 2026 at 1:52 pm

    Largely edited my answer as I got it wrong.

    Using f.select, you’ll be able to provide an Array containing exactly what you need.
    Assuming that Invoice.due returns your desired collection of Invoice

    Here goes :

    f.select(:invoice_id, Invoice.due.map {|i| ["#{i.number} #{money(i.amount_due)}", i.id]})
    

    I think that collection_select can’t achieve what you’re trying to do, unless you add the helper containing money to your model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this a proper way or are there better solutions? Thanks a lot! Code
Is there a better way to code this? def __contains__(self, e): return e in
Is there a better way to write this spec? This works but I don't
This might be a stupid question but if there's a better or proper way
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
I'm binding ItemsControl to CollectionViewSource. Here is code: this.Trucks = new ObservableCollection<Truck>(); foreach (var
Say I have the following code using the Preferences class. Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
Better way to do it (without QImage )?: QImage image(width, height, QImage::Format_RGB888); memcpy(image.bits(), m_frameRGB->data[0],
Is it better / faster inside an event listener to use this or event.target
So I have some code, <div class=chunk id=> <div class=chunkContent> <div class=textLeft ></div> <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.