Topic: problem with CRUD operation

Hi, I'm RoR newbie... I'm trying to make couple database CRUD operation in couple file... each file "maintain" one database. The first database (Admin database) is working fine, but not the second (Projectmanager). When I'm trying to do update (updateprojectmanager) operation (I'm not yet coding the other operation) on the second database, the value didn't updated. Here is chunk of my code

admin_controller.rb

        def index
		@admins = Admin.find_all
		@projectmanagers = Projectmanager.find_all
		@developers = Developer.find_all
	end
	
	#fungsi bagi admin
	def maintainadmin
		@admins = Admin.find_all
	end
	
	def editadmin
		@admin = Admin.find(@params[:id])
	end
	
	def destroyadmin
		Admin.find(@params[:id]).destroy
		redirect_to :action => 'maintainadmin', :id => @admin
	end	
	
	def updateadmin
		@admin = Admin.find(@params[:id])
		if @admin.update_attributes(@params[:admin])
			redirect_to :action => 'maintainadmin'
		else
			render_action 'editadmin'
		end
	end
	
	def newadmin
		@admin = Admin.new
	end
	
	def createadmin
		@admin = Admin.new(@params[:admin])
		if @admin.save
			redirect_to :action => 'maintainadmin'
		else
			render_action 'createadmin'
		end
	end
	
	#fungsi bagi project manager
	def maintainprojectmanager
		@projectmanagers = Projectmanager.find_all
	end
	
	def editprojectmanager
		@projectmanager = Projectmanager.find(@params[:id])
	end
	
	def updateprojectmanager
		@projectmanager = Projectmanager.find(@params[:id])
		if @projectmanager.update_attributes(@params[:projectmanager])
			redirect_to :action => 'maintainprojectmanager'
		else
			render_action 'editprojectmanager'
		end
	end

here is chunk of index.rhtml

<table border="1">
 <tr>
  <td width="80%"><p align="center"><i><b>Project Manager</b></i></td>
 </tr>

 <% @projectmanagers.each do |projectmanager| %>
  <tr>
   <td><%= projectmanager.username %></td>
  </tr>
 <% end %>
</table>
<%= link_to 'Maintain Project Manager Account', :action => 'maintainprojectmanager' %>

here is maintainprojectmanager.rthml

<h1>Maintain Project Manager Account</h1>
<table>
<tr>
<% for column in Projectmanager.content_columns %>
<th><%= column.human_name %></th>
<% end %>
</tr>
<% for projectmanager in @projectmanagers%>
<tr>
<% for column in Projectmanager.content_columns %>
<td><%=h projectmanager.send(column.name) %></td>
<% end %>
<td><%= link_to 'Edit', :action => 'editprojectmanager', :id => projectmanager %></td>
<td><%= link_to 'Destroy', {:action => 'destroyprojectmanager', :id => projectmanager}, :confirm =>
"Are you sure?" %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Project Manager', :action => 'newprojectmanager' %> |
<%= link_to 'Back', :action => 'index' %>

here is the editprojectmanager.rhtml

<% @heading = "Edit Project Manager Account" %>
<%= start_form_tag :action => 'updateprojectmanager', :id => @projectmanager%>
<%= render_partial "formprojectmanager" %>
<hr />
<%= submit_tag "Save" %>
<%= end_form_tag %>
<%= link_to 'Back', :action => 'maintainprojectmanager' %>

I'm sorry if my post is too long, it is because I don't know where the bug is. The browser didn't give any error sign. Can anybody give me a hint? Thanks for the respond smile

Re: problem with CRUD operation

Can I ask why you are using two different databases?

Re: problem with CRUD operation

Ugh. Where to begin. First of all, you're working against all kinds of conventions. But I won't get into that. Try removing the @ sign from @params. So it should look like

projectmanager.update_attributes(params[:projectmanager])

instead of

projectmanager.update_attributes(@params[:projectmanager])
Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: problem with CRUD operation

@Ace of Dubs: Ooops my mistake... I mean couple tables in one database. Sorry...
@ryenski: Wow am I doing that bad? Well I tried learning about rails from here. Can you reccomend me resource for rails newbie (like site and soon)... because indeed I'm new about all this conventions (just 2 days learning), and attracted as well big_smile. Btw I tried your suggestion but nothing change.

Re: problem with CRUD operation

After I'm doing some googling... I notice what you mean by "against all kinds of conventions"... Damn I need to understand more about all this conventions... So I guest no other option to rebuild the application from scratch again. So I take my first question back, but still I want to know about where to find good resources for RoR newbie (free e-book would be nice), can anyone help me?

Re: problem with CRUD operation

Hi Fcarvis - sorry to be so obtuse earlier. One of the best resources to learn about Rails is Agile Web Development with Rails. They'll take you from beginning to learn about Rails through advanced techniques. I also have a book list on my website at http://www.artofmission.com/about/books .

Ryan Heneise  |  Art of Mission  |  Now with extra-strong Donor Tools mojo

Re: problem with CRUD operation

A couple of conventions you should start adopting

1. When defining method names, use underscores to separate words (def this_action)
2. Your controllers do not need the name of the model in every action. editadmin, newadmin, etc should just be edit, new, etc. They are kept within the scope of a given controller so it doesnt matter if the method "new" is in all of your controllers

For a good idea of default CRUD actions, try using rails resource generator. In the command line, cd to your project folder then run this:

script/generate scaffold_resource ModelName

That single line will spit out your basic controllers, models, views, tests, migrations...the works. Poke around in those files to get a feel for Rails conventions.

3. start_form_tag and end_form_tag are being phased out of Rails. Look upform_for as an acceptable alternative.

As for learning resources, ryenski's book list is spot on. AWDWR is the first one you should check out, followed by Ruby for Rails and perhaps Rails Recipes (although some of the code is a bit dated now, it is chock full of goodies)

If you prefer a searchable API, look no further than http://labs.parkerfox.co.uk/ruby.search/

The good folks over at Rails Forum are also very helpful.

Good luck, and welcome aboard smile