kludge for drop-down list

The following is a kludge to generate a drop down list of the rows for the "categories" table (from the "Category" model).

thufir@arrakis ~/Desktop/strawr $ thufir@arrakis ~/Desktop/strawr $ tail app/views/feeds/show.rhtml -n 2

<%= select(:category, :id, Category.find(:all).collect {|c| [ c.category, c.id ] } ) %> thufir@arrakis ~/Desktop/strawr $

However, I'd rather put a method in feeds_controller.rb if that's possible. Would that be a better way to generate the drop down list?

thanks,

Thufir

If you want to wrap this up in a method put it in feeds_helper.rb

Fred

I've done an admittedly brief google and haven't found much. Am I on the right track?

http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html

thanks,

Thufir

If you want the helper to be available to all templates in the app, define it in application_helper.rb otherwise define it in category_helper.rb.

PLEASE! Do not put model code in the view. It's like putting onion in ice cream.

I'm not clear on what you mean. The attributes/fields for the model are defined at http://strawr.googlecode.com/svn/trunk/db/migrate/002_feeds.rb, not really in http://strawr.googlecode.com/svn/trunk/app/models/feed.rb. However, I suppose that's still within the "model" for MVC.

The point is to have the drop-down list at http://strawr.googlecode.com/svn/trunk/app/views/feeds/show.rhtml give the same result as

Category.find :all

=> [#<Category:0xb700b90c @attributes={"category"=>"some category", "id"=>"1"}>]

does from script/console, albeit without the hexadecimal.

I understand the generalities, that in MVC the different parts are intentionally not overlapping. Yes, the view should stay with view stuff. Concretely, what do you mean?

-Thufir

Yes, I want to edit <http://strawr.googlecode.com/svn/trunk/app/helpers/ application_helper.rb> to, umm, help the controller(s) list the categories through the various show.rhtml's, but in a correct, non-kludgy way.

Pardon, no I haven't done much research into how to do that specific operation, but could I have some direction as to how to do that? I want to render the views without going back to the database.

thanks,

Thufir

[...]

If you want to wrap this up in a method put it in feeds_helper.rb

[...]

I would like to wrap the it up in a method in application_helper.rb but don't from reading http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html and other sources don't know what method to create.

I want a drop down list for the model http://strawr.googlecode.com/svn/trunk/app/models/category.rb to replace the kludge to generate that list, as it's currently done, at http://strawr.googlecode.com/svn/trunk/app/views/feeds/show.rhtml .

How does the method in application_helper.rb get "called"?

thanks,

Thufir

You can put whatever you want in feeds_helper, application helper etc... Those modules are included in view templates as appropriate.

So if you have module FeedsHelper    def my_helper    end end

You can do <%= my_helper %> in your view. From a helper you can call all the helper functions you normally have access to.

Fred

[...]

So if you have module FeedsHelper    def my_helper    end end

You can do <%= my_helper %> in your view. From a helper you can call all the helper functions you normally have access to.

Fred

Thanks, Fred, that helps quite a bit. Something like:

module FeedsHelper   def list_categories      Category.find(:all)   end end

-Thufir

[...]

So if you have module FeedsHelper   def my_helper   end end

You can do <%= my_helper %> in your view. From a helper you can call all the helper functions you normally have access to.

Fred

Thanks, Fred, that helps quite a bit. Something like:

module FeedsHelper def list_categories     Category.find(:all) end end

You're on the way there. If it was me then I'd have a method on categories that did the find
for me, with the right conditions etc... Then in Feeds Helper i'd have

def select_category    select :category, :id, Category.some_find_method.collect {|c|
[ c.category, c.id ] end

Fred

I think that I would get better results if I tried to do this at three pm instead of three am!

Ok, I got it working, but it feels equally kludgy:

thufir@arrakis ~/Desktop/strawr $ thufir@arrakis ~/Desktop/strawr $ cat app/helpers/application_helper.rb # Methods added to this helper will be available to all templates in the application. module ApplicationHelper

def select_category # select :category, :id, Category.some_find_method.collect {|c| [ c.category, c.id ]}         select :category, :id, Category.find(:all).collect {|c| [ c.category, c.id ]}

end end

thufir@arrakis ~/Desktop/strawr $ thufir@arrakis ~/Desktop/strawr $ tail app/views/feeds/show.rhtml -- lines=1 <%= select_category %> thufir@arrakis ~/Desktop/strawr $

However, that achieves the purpose. I think what bothers me, slightly, is the complexity of the compound line in the select_category method. I can read it, sorta, but can't write it :frowning:

However, I'll live :slight_smile:

Oh, now I get it. I must be tired. I need to define a few "some_find_method"'s, like: categories linked to this feed, categories not linked to this category, etc. These methods are in the same module?

I was thinking of refining the query from all records to just those records which are linked to the particular object. However, since I have trouble writing that in English, I'll take a pass and come back to that.

I would like have two buttons: "link" and "unlink".

When "link" is clicked then a row the "tags" table (horrible name) is created with the feeds_id and categories_id values. (If it creates the same relationship ten times, that's ok for now). The unlink would delete the corresponding row from the "tags" table.

I followed the Apple demo and created Google Code Archive - Long-term storage for Google Code Project Hosting. expenses/ , which has buttons kind-of along this line. However, it's a one-to-many wheras I want a many-to-many. Plus, I think that the Apple tutorial is more than a little dated.

Just thinking outloud. What kind of button would be sufficient for what I describe? I'm a bit more confident with how the show.rhtml interacts with methods, but: which method in which class for a "submit" type button? where does the button go?

Just thinking out loud, and will be googling tomorrow :slight_smile:

-Thufir

Right; for now it's ok and I want to move forward. In which file would you put this query?

thanks,

Thufir