Topic: Link to different controllers referencing different fields

I am trying to build a button that takes the list views column fields :barcode key and look up on the infos tables :barcode fields display

My link to code in display

            <td><%= link_to 'Show/Edit', :controller => 'infoadmin', :action => 'buttons', :id => i, :numx => :barcode  %></td>

And the controller it sends it's singal back to
def buttons
    #buttons = Info.new
#    @buttons = Info.find(:all, :conditions => "address_track = 'bun' ")
barcode = params[:numx]
    @buttons = Info.find(:all, :conditions => ["barcode = ?", %{'barcode'}])
end

I can't get it to find the param of the field.. I can get it to work just fine on the id but I need to use the barcode instead in this case.

as the infos has_many :medialogs

Last edited by tripdragon (2006-09-05 17:06:25)

Re: Link to different controllers referencing different fields

not sure if this will solve your problem, but try:

@buttons = Info.find(:all, :conditions => ["barcode = ?", params[:numx]])

Railscasts - Free Ruby on Rails Screencasts

Re: Link to different controllers referencing different fields

no it did not, I keep getting it sent as text in my url with nothing displaying

http://localhost:3000/infoadmin/buttons/1?numx=barcode
If I use the actual number in place of 'barcode' like '6501' it just finds that record and displays it, so I know it works , just not with the custom param being sent.



and a very simple view to show anything

<table>
<% @buttons.each do |g| %>

<tr>
    <td>ID <%= g.id %></td>
    <td>Barcode <%= g.barcode %></td>
    <td>Address <%= g.address_track %></td>

</tr>
<% end %>

</table>

Last edited by tripdragon (2006-09-05 17:52:02)

Re: Link to different controllers referencing different fields

Oh, I see, right now numx is statically set to barcode in the link_to method, but you want it to be the barcode number (a variable), not the word 'barcode'.

I would need to see more of the code surrounding the link_to method in order to help you with this. If the 'i' variable is a model with a barcode field, you could do this:

<td><%= link_to 'Show/Edit', :controller => 'infoadmin', :action => 'buttons', :id => i, :numx => i.barcode  %></td>

I don't know if that's the case though without seeing more of the code.

Last edited by ryanb (2006-09-05 18:27:49)

Railscasts - Free Ruby on Rails Screencasts

Re: Link to different controllers referencing different fields

ryanb wrote:

<td><%= link_to 'Show/Edit', :controller => 'infoadmin', :action => 'buttons', :id => i, :numx => i.barcode  %></td>

I don't know if that's the case though without seeing more of the code.

That did it!

But just incase

First view that sends the link

<% if @total == 0 %>

<p>No medialogs found...</p>

<% else %>

<p>Number of medialogs found : <b><%= @total %></b></p>

<p>
<% if @medialogs_pages.page_count > 1 %>
Page&nbsp;:
<%= pagination_links_remote @medialogs_pages %>
<% end %>
</p>


<table>
    <thead>
        <tr>
            <td <%= sort_td_class_helper "id" %>>
                <%= sort_link_helper "id", "id" %>
            </td>
            <td <%= sort_td_class_helper "barcode" %>>
                <%= sort_link_helper "barcode", "barcode" %>
            </td>
            <td <%= sort_td_class_helper "shot_type" %>>
                <%= sort_link_helper "shot_type", "shot_type" %>
            </td>
            <td <%= sort_td_class_helper "shot_description" %>>
                <%= sort_link_helper "shot_description", "shot_description" %>
            </td>
            <td <%= sort_td_class_helper "start" %>>
                <%= sort_link_helper "start", "start" %>
            </td>
            <td <%= sort_td_class_helper "end" %>>
                <%= sort_link_helper "end", "end" %>
            </td>
            <td <%= sort_td_class_helper "duration" %>>
                <%= sort_link_helper "duration", "duration" %>
            </td>
            <td <%= sort_td_class_helper "searched_by" %>>
                <%= sort_link_helper "searched_by", "searched_by" %>
            </td>
        </tr>
    </thead>
    <tbody>
        <% @medialog.each do |i| %>
        <tr class="<%= cycle("even","odd") %>">
            <td><%= i.id %></td>
            <td><%= i.barcode %></td>
            <td><%= i.shot_type %></td>
            <td><%= i.shot_description %></td>
            <td><%= i.start %></td>
            <td><%= i.end %></td>
            <td><%= i.duration %></td>
            <td><%= i.searched_by %></td>
            <td><%= link_to 'Show/Edit', :controller => 'infoadmin', :action => 'buttons', :id => i, :numx => i.barcode  %></td>
           
         </tr>
        <% end %>
    </tbody>
</table>

<% end %>


Controller
def buttons
    #buttons = Info.new
#    @buttons = Info.find(:all, :conditions => "address_track = 'bun' ")
#barcode = params[:numx]
    #@buttons = Info.find(:all, :conditions => ["barcode = ?", %{'barcode'}])
    @buttons = Info.find(:all, :conditions => ["barcode = ?", params[:numx]])

   
end


Second view that renders
<table>
<% @buttons.each do |g| %>

<tr>
    <td>ID <%= g.id %></td>
    <td>Barcode <%= g.barcode %></td>
    <td>Address <%= g.address_track %></td>

</tr>
<% end %>

</table>


very basic prime stuff,, but I just want to make sure I have a real working knowledge of wtf is going on in the code.
I read that teh find stuff goes in the model So should I try and move it there ? It's such a small string that I just need to pull up one unique record that will have lost of other records... So I might have to do it in sql later but for now it works and I am happy so alls fine by me big_smile

Re: Link to different controllers referencing different fields

tripdragon wrote:

I read that teh find stuff goes in the model So should I try and move it there ? It's such a small string that I just need to pull up one unique record that will have lost of other records... So I might have to do it in sql later but for now it works and I am happy so alls fine by me big_smile

You should leave the find in the controller since it's so simple. If you ever make it more complicated, then you may want to consider moving it into the model. You may also want to consider moving it into the model if you do the same find in multiple controllers (to remove the duplication).

Railscasts - Free Ruby on Rails Screencasts