Sunday, August 31, 2008

Nested Urls

If you would like to nest resources, for example:

http://localhost:3000/school_levels/1/school_level_specifications:

You will need to do the following:

  1. Setup your routes file
  2. Setup your models so that they describe the relationship
  3. Setup the nested controller (school_level_specifications) to grab an instance of the parent object (school_levels/1).









Here's some good resources on nesting urls (including how form_for gets modified)
http://adamblog.heroku.com/past/2007/12/20/nested_resources_in_rails_2/

If you want to use link_to, use the following:
<%= link_to 'Show', [@daily_content, vocabulary] %>


If you want to add extra information in a form_for use a text_field_tag. If that tag is in a nested resource, you can access the information using two levels of the params variables. See below.

If you use nested resources, a

<%= text_field_tag 'user[firstname]' %>

in your form, will result in

params[:user][:firstname]

in your controller.

To find the naming scheme use: rake routes

Saturday, August 30, 2008

Style Sheets

When you want to use a style sheet, there are three things to remember
  1. Create the style sheet in public/stylesheets/ directory.
  2. Link to the stylesheet (usually in the app/views/layouts/model_name.erb file. (You should have one .erb per model available.
  3. Use the style sheets by appending the class attributes to the xml tags in your xhtml.

Wednesday, August 27, 2008

Ruby Forms using select, textfields etc.

If you would like to connect form objects (textfields, selects etc.) to a field. You will want to wrap those fields in a form_for.

By using f.text_field and f.select you are don't have to worry about including the logic to populate your text_field (if the object exists) or select your option (if the object exists).

Note the select option. You usually create a select option by doing something like f.select (:school_level_id, [ ["First Years", 1] ["Second Years", 2] ]). The first parameter (:school_level_id) is a field in your model. The second parameter is a list of your options. Note that you can automatically create that list of lists using pretty ruby syntax like SchoolLevel.find(:all).collect { |c| [c.name, c.id] }).

If you want to set a default value:
# in controller which manages view described above
@work.project_id = params[:pid] unless params[:pid].nil?



For another brief summary like this, see here.

Saturday, August 23, 2008

Custom Actions

Adding Custom Actions

Let's say that you have a students model/view/controller. You can start by doing things like:




http://localhost:3000/studentsuses index.html.erb, def index
http://localhost:3000/students/newuses new.html.erb, def new
http://localhost:3000/students/1uses show.html.erb, def new
Let's say you also want to get one of the following urls working ...
  • http://localhost:3000/students/feedme
  • http://localhost:3000/students/1/feedme

What's happening is that you are requesting a custom action called "feedme." (Normally Ruby's restful interface only allows 7 actions, see more commentary below).

Let me show you how to add custom actions.

Getting http://localhost:3000/students/feedme Working

Step 1:
To class StudentsController add a method:
def feedme
#do something here...
end

Step 2:
To app/views/students/ folder add the following file:
feedme.html.erb
and write the html you want to execute.

Step 3:
To your routes.rb file add
map.resources :students, :collection => {:feedme => :get}

Read the above line something like this: Add a custom action called "feedme" that empoys a http "get" request on the students model.

Note that you will likely already have "map.resources :students" in the routes.rb file to start.

Getting http://localhost:3000/students/1/feedme Working

Do the exact same steps except the routes.rb file should include

map.resources :students, :member => {:feedme => :get}


WARNING

Creating a custom action ought to be done sparingly. Ruby's latest incarnation enforces a restful architecture - part of which includes a uniform interface for all your controls. When you add custom controls you break the uniform interface on this model - so do it only when necessary!

Futher resources: