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 :


<%= link_to ‘New user’, new_user_path %>

<%= link_to ‘paris’,{:controller => “users”, :action => “paris” }%>


I added the “Paris” control in “users_controller.rb” wich I left empty
but I get the same error no matter what I put in it…


def paris

end


Everytime I click on the link I get the following error :


"ActiveRecord::RecordNotFound in UsersController#show

Couldn’t find User with ID=paris"


I am unable to add new controls as I always get “Couldn’t find User with
ID=paris” no matter what I put in the Paris action.

There i probably some RoR concept I didn’t get right…

thanx for your help !

[email protected]

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:

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 %>

Leo K. 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.

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

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 :


class ParisController < ApplicationController

def index
#put your database code here
end

end


I still get the same error O_<

“Couldn’t find User with ID=paris”

On May 12, 2008, at 2:58 PM, AndyV wrote:

On May 12, 2:33 pm, Matt M. [email protected]

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

Hope this works for you

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 B. http://agileconsultingllc.com
[email protected]

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 M. [email protected]

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 M. [email protected]

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.

thank you so much Matt M…

This forum is really helpfull to beginners as there isn’t that much good
RoR documentation on the net (or perhaps I’m not going on the right
sites…)

Does anybody know about a good lightweight editor where I can open and
edit my RailsApps from my instantrails directory? Because I’m working
with notepad++ :confused:

thanks !

Anyways everything works now so thank you all

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!

On 5/12/08, Leo K. [email protected] wrote:

This forum is really helpfull to beginners as there isn’t that much good
RoR documentation on the net (or perhaps I’m not going on the right
sites…)

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.


Greg D.
http://destiney.com/

Leo K. 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.

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?