0

I want the Link text on my view to display as Hospital, Country. Country is a guidelines attribute so I need to be able to access guidelines.country from 'hospital' and display it

e.g. Get Well Hospital, Sickland

I'm not sure how to code this correctly. At the moment in my view file I have

<% @list.each do |hospital| %>

        <tr class="tablerow">
            <td><%= link_to (hospital, country), :action => :topichospital, :hospital => hospital, :country=>country %></td>
        </tr>

It worked when I just had, but I'm not sure how to add the country too

 <% @list.each do |hospital| %>

            <tr class="tablerow">
                <td><%= link_to hospital, :action => :topichospital, :hospital => hospital %></td>
            </tr>

my listhospital action in guidelines_controller.rb is

def listhospital
    @list = Guideline.order(:hospital).uniq.pluck(:hospital)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @guidelines }
    end
  end
2
  • Where is the country variable coming from? It's probably not stored in @list as i'm guessing that @list is just an array of hospital names. Commented Mar 18, 2013 at 0:30
  • aha! You are right. I have added my action in the controller. @list is just an array of hospitals so how would I access guidelines.country from this (both country and hospital are attributes of guidelines models)? Commented Mar 18, 2013 at 0:42

2 Answers 2

1

change your link_to to

<%= link_to "#{hospital}, #{country}", { :action => :topichospital, :hospital => hospital, :country=>country } %>

This will convert the first parameter passed to string. I'm not sure how rails interprets (hospital, country) in a link_to when passed as the first parameter but this will make sure to call the to_s methods for each.

UPDATE: IIRC, you can use pluck to combine attributes

# postgre
@list = Guideline.order(:hospital).uniq.pluck("hospital || ', ' || country")

# mysql
@list = Guideline.order(:hospital).uniq.pluck("CONCAT(hospital, ', ', country)")

then you can just use link_to hospital in the view.

UPDATE: This is becoming a bit of a hack. I suggest you change the controller to

@list = Guideline.select('hospital, country').order(:hospital).uniq

Then in your view

<% @list.each do |guideline| %>
  <tr class="tablerow">
    <td><%= link_to "#{guideline.hospital}, #{guideline.country}", { :action => :topichospital, :hospital => guideline.hospital, :country => guideline.country }%></td>
  </tr>
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

yes, this has solved that problem. as you can see from my edit above, the other issue is that I need to work out how to access country from the list of hospitals...
that works perfectly in the sense that it displays in the view correctly. But it's posing a problem for what it's actually linking to. I want it to display like this on the page but when it load the link it needs to just link to /guidelines/topichospital?hospital=Hospital+Name rather than /guidelines/topichospital?hospital=Hospital+Name&country
0

I think you're looking for:

<%= link_to "#{hospital}, #{country}", :action => :topichospital, :hospital => hospital, :country=>country %>

You could also pass a block to link_to:

<%= link_to :action => :topichospital, :hospital => hospital, :country=>country do %>
  <%= hospital %>, <%= country %>
<% end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.