Beginner's problem :p

Hi all, I'm new to RoR

I finally got a RoR environment working on my PC with instantrails 2.0.

Here is my problem.

I created a database with a "Users" table. It's structure is the following :

Name, Surnmame, City, age...

The table containing the Users was succesfully scafolded and I have the adress "localhost:3000/users" running with all the CRUD functions ! :smiley:

now I want to add a link at the botom of the index page allowing me to filter the users living in Paris (and ultimately, a textbox where I can put in any search criteria). But getting the filter for the Paris Users would already end a huge headache.

I added at the bottom of the index page a "Paris" link after the prebuilt "new user" link.

the bottom of my index page looks like this :

Try it like this.

<%= link_to "Paris", :controller => :users, :action => :paris %>

Brandon Roberts wrote:

Try it like this.

<%= link_to "Paris", :controller => :users, :action => :paris %>

I get the same exact error :confused:

Leo Kowalsky wrote:

Brandon Roberts wrote:

Try it like this.

<%= link_to "Paris", :controller => :users, :action => :paris %>

I get the same exact error :confused:

Hhmm, I'm a noob myself but the way I usually take care of something like this is to generate a new controller and put my code there. I find this is the easiest way for me since then I can just restrict access to the CRUD that scaffold generated for me and use it as part of my Admin area.

Brandon Roberts wrote:

I can verify that this does work although it probably isn't DRY enough for most rails developers.

Open terminal in project directory and run: script/generate controller paris

add the following to the controller: def index   #put your database code here end

and now your link becomes:

<%= link_to :controller => :paris %>

I put

<%= link_to 'paris', controller => :paris %>

in my index.html.rb

and "paris_controller.rb" contains :

Your problem is with your routes, you have the standard restful routes setup so it's interpretting the action as an id. I would suggest using GET variables to do the filter. I would use something like this

<%= link_to "Users", :action => "index", :filter => "Paris" %>

now in your index action you do something like

if(params[:filter])   @users = User.find(:all, :conditions => "users.city = '#{params[:filter]}'") else   @users = User.find(:all) end

Hope this works for you

I agree with Matt. "filter" and "search" are really indexes with more specific criteria. If you take the approach he's suggesting you keep the footprint smaller.

@Brandon: Speaking of not DRY... aren't you logically suggesting that he create a new controller for every interesting city he can think of? You're repeating all the lookup code, the routes, etc simply for the sake of a simple, pretty url. Keeping things DRY is a principle that crosses the boundaries of classes.

You'd be better off specifying the conditions as:

   :conditions => { :city => params[:filter] } or    :conditions => ['city = ?', params[:filter] ]

to avoid SQL Injection attacks.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

AndyV wrote:

I agree with Matt. "filter" and "search" are really indexes with more specific criteria. If you take the approach he's suggesting you keep the footprint smaller.

@Brandon: Speaking of not DRY... aren't you logically suggesting that he create a new controller for every interesting city he can think of? You're repeating all the lookup code, the routes, etc simply for the sake of a simple, pretty url. Keeping things DRY is a principle that crosses the boundaries of classes.

On May 12, 2:33 pm, Matt Mongeau <rails-mailing-l...@andreas-s.net>

No, not exactly. My background is php and in learning rails I found that it made more sense to me if I just left the scaffold pages "as is" but add a berfore filter to restrict access to them. This give me my "admin" functionally that I normally require. As for the extra controller I normally make an extra controller that can accept parameters and be dynamic. I use this controller to display my pages that the end users see. The example I described is not how I normally do things but was more of an example of a method he could use to get past the problem he was having.

Anyways everything works now so thank you all

I find this very helpful:

http://start.gotapi.com/

I set the check boxes depending on what I'm working on that day or week or whatever.

Thanks for all your help.

I am wondering now how I can put a searchbox at the botom of the index page to search through the users (by age, city ect...)

I tried to inspire myself with the content in "new.html.erb" for the textbox and submit button but I'm not getting anywhere...

thanks in advance!

I put

<%= text_field_tag :query %>

at the bottom of my page but how do I add a button to handle the content of the searchbox so that I can use it as filter?

Leo Kowalsky wrote:

I put

<%= text_field_tag :query %>

at the bottom of my page but how do I add a button to handle the content of the searchbox so that I can use it as filter?

Here is the code for one of my search forms

      <% form_tag posts_path, :method => 'get' do %>         <% content_tag :label do %>           <%= text_field_tag :search, params[:search] %>       <%= submit_tag "Search" %>         <% end %>       <% end %>

Here is the relevant section from tho controller:

  def index     @posts = Post.search params[:search], params[:page]     if params[:category_id] != nil       @posts = Post.category params[:category_id], params[:page]     end     respond_to do |format|       format.html # index.html.erb       format.xml { render :xml => @posts }     end   end

Just a side note but I am also using mill_paginate in this program.