First problem and first mail

6 views
Skip to first unread message

mujahid khan

unread,
Jan 27, 2010, 1:13:28 AM1/27/10
to rubyonrails...@googlegroups.com
Dear all

Now i am a group member with you. I am trying to built a application with Ruby on Rails. Now i am facing some problems. I have created a table named regions. The primary key is region_id (not default id). when i insert data from view data has inserted but not displayed. My new.html code is:

<h1>New region</h1>

<% form_for(@region) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :region_type %><br />
    <%= f.text_field :region_type %>
  </p>
  <p>
    <%= f.label :region_name %><br />
    <%= f.text_field :region_name %>
  </p>
  <p>
    <%= f.label :region_country %><br />
    <%= f.text_field :region_country %>
  </p>
  <p>
    <%= f.label :region_parent %><br />
    <%= f.text_field :region_parent %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', regions_path %>

after submitting the page shows a error:

ActiveRecord::RecordNotFound in RegionsController#show

Couldn't find Region without an ID
....
.....

Request

Parameters:

{"id"=>"0"}

but there is no field named id in my database table. the show method in my regions_controller file is:
 def show
    @region = Region.find(params[:region_id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @region }
    end
  end

i have generated the files using scaffold.

please give me the solution. i don't know from where id is passing as a parameter.

from a new member
mujahid 


 


Get your new Email address!
Grab the Email name you've always wanted before someone else does!

S. M. Sohan

unread,
Jan 27, 2010, 1:46:49 AM1/27/10
to rubyonrails...@googlegroups.com
Dear Mr. Mujahid:
I see two issues here:
  1. The default routing system in rails maps urls like /regions/1 params = {:id => 1} so, although you have the primary key region_id this is not the case for the params hash. You could simply use Region.find(params[:id]).
  2. However, if you are using auto increment ids, I think most databases start the ids at 1. So, in the example you gave, I see the id has a value=0. Can you double check if you actually have a record in the table with region_id = 0?
Hope it solves your problem.

Thank you.
--
Sohan
http://www.ucalgary.ca/~smsohan | http://smsohan.blogspot.com
skype:smsohan | gtalk:sohan39 | ph: 403-351-0718



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails Bangladesh" group.
To post to this group, send email to rubyonrails...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails_bangl...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails_bangladesh?hl=en.

mujahid khan

unread,
Jan 27, 2010, 1:53:33 AM1/27/10
to rubyonrails...@googlegroups.com
Dear Mr. Sohan

Thank you for your reply. But region_id is created by a trigger. The region_id is for Division, District and PS. Division ID start from: 1000000, District ID start from: 1100000 and PS ID start from:1110000.

so i don't know from where id is passing as a parameter. if it default how can i change it? please help me.

thank you

--- On Wed, 27/1/10, S. M. Sohan <soh...@gmail.com> wrote:

Ashrafuzzaman

unread,
Jan 27, 2010, 4:47:04 AM1/27/10
to rubyonrails...@googlegroups.com
Hi Mr. Mujahid,
In addition to what Sohan vai said, I think if you want to use a custom primary key in rails you need to specify that in the model, like
class Region < ActiveRecord::Base
  set_primary_key :region_id
  ...
end

Pleas check http://www.edwardbenson.com/2009/06/12/custom-primary-keys-with-activerecord/

mujahid khan

unread,
Jan 27, 2010, 4:59:11 AM1/27/10
to rubyonrails...@googlegroups.com
Dear Mr. Ashrafuzzaman

Thank you toooo much. problem solved within 30 seconds!!!.


mujahid

--- On Wed, 27/1/10, Ashrafuzzaman <ashrafuz...@gmail.com> wrote:


New Email names for you!
Get the Email name you've always wanted on the new @ymail and @rocketmail.
Hurry before someone else does!

Ashrafuzzaman

unread,
Jan 27, 2010, 5:04:07 AM1/27/10
to rubyonrails...@googlegroups.com
glad to help :)
Reply all
Reply to author
Forward
0 new messages