Newbie: Why doe New work but edit/update not?

It seems rather odd but my application will happily allow me to add new
records via my form, but using the same form partial the edit action
seems to work fine (It throws no errors) but completely fails to change
the database in any way. Do I need to add a @users.save command or
something?

Here is my UsersController code

class UsersController < ApplicationController

GET /users

GET /users.xml

def index
if params[:usersearch].blank?
@users = User.find(:all)
else

    @users = User.find(:all, :conditions => ['Surname LIKE ? or

Forename LIKE ? or Position_Group LIKE ?’, “%#{params[:usersearch]}%”,
“%#{params[:usersearch]}%”, “%#{params[:usersearch]}%”])
end

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @users }
end

end

GET /users/1

GET /users/1.xml

def show
@user = User.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @user }
end

end

GET /users/new

GET /users/new.xml

def new
@user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @user }
end

end

GET /users/1/edit

def edit
@user = User.find(params[:id])
end

POST /users

POST /users.xml

def create
@user = User.new(params[:user])

respond_to do |format|
  if @user.save
    flash[:notice] = 'User was successfully created.'
    format.html { redirect_to(@user) }
    format.xml  { render :xml => @user, :status => :created,

:location => @user }
else
format.html { render :action => “new” }
format.xml { render :xml => @user.errors, :status =>
:unprocessable_entity }
end
end
end

PUT /users/1

PUT /users/1.xml

def update
@user = User.find(params[:id])

respond_to do |format|
  if @user.update_attributes(params[:user])
    flash[:notice] = 'User was successfully updated.'
    format.html { redirect_to(@user) }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @user.errors, :status =>

:unprocessable_entity }
end
end
end

DELETE /users/1

DELETE /users/1.xml

def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
  format.html { redirect_to(users_url) }
  format.xml  { head :ok }
end

end
end

What does the form partial look like?

Michael B. wrote:

What does the form partial look like?

here you go

<%= error_messages_for :user %>

<% form_for(@user) do |f| %>

Personal Details

ImageName
<%= f.text_field :ImageName %>
Surname
<%= f.text_field :SurName %>
Forename
<%= f.text_field :ForeName %>
Title
<%= f.text_field :Title %>
Room No
<%= f.text_field :RoomNo %>
Phone No
<%= f.text_field :PhoneNo %>
EMail
<%= f.text_field :EMail %>

<%= f.submit "Save" %>

<% end %>

Do I need to add a @users.save command or something?

update_attributes returns true if the save was successful.

On Jun 4, 6:57 am, Dale C. [email protected]

Michael B. wrote:

What does the form partial look like?

I’ve also noted that the destroy option doesn’t delete records either.

You say that you don’t get any errors but what happens when you hit
save? Does it redirect to the edit or show action?

On Jun 4, 8:51 am, Dale C. [email protected]

Mike Breen wrote:

You say that you don’t get any errors but what happens when you hit
save? Does it redirect to the edit or show action?

On Jun 4, 8:51 am, Dale C. [email protected]

It redirects to the show action.

hmmm…did you check the log or watch the output of script/server?

Parameters: {“user”=>{“CorrectEditedForeName”=>"",
should be:
{“user”=>{“ForeName”=>“CorrectEditedForeName”,

Michael B. wrote:

hmmm…did you check the log or watch the output of script/server?

Here is the last entry the one that occurs when I click the save button.

Processing UsersController#index (for 134.225.101.126 at 2008-06-04
16:00:10) [PUT]
Session ID: random stuff
Parameters: {“user”=>{“CorrectEditedForeName”=>"",
“SurName”=>“CorrectEditedTestName”, PhoneNo"=>“6609”, “Duties”=>"",
“RoomNo”=>“1L38”, “commit”=>“Save”, authenticity_token"=>“random digits
ive removed”, “_method”=>“put”, “action”=>“index”,
“controller”=>“users”}
e[4;36;1mUser Load (0.431220)e[0m e[0;1mSELECT * FROM users e[0m
Rendering template within layouts/users
Rendering users/index
e[4;35;1mUser Columns (0.013844)e[0m e[0mSHOW FIELDS FROM
userse[0m
Completed in 1.15826 (0 reqs/sec) | Rendering: 0.52547 (45%) | DB:
0.44506 (38%) | 200 OK [http://webaddress/users/users/]

It seems to look ok no?

John Y. wrote:

Parameters: {“user”=>{“CorrectEditedForeName”=>"",
should be:
{“user”=>{“ForeName”=>“CorrectEditedForeName”,

Yep it is, my paste is not precise since I’ve edited it to protect the
innocent :smiley:

I wouldn’t have thought the error was going to be in the data.

Are your routes set up properly? What’s your output from rake routes?

(I’m just going through the normal troubleshooting steps I would take
to track this down. if you don’t think I’m helping please feel free to
ignore me :wink:

Michael B. wrote:

Are your routes set up properly? What’s your output from rake routes?

(I’m just going through the normal troubleshooting steps I would take
to track this down. if you don’t think I’m helping please feel free to
ignore me :wink:

          users GET    /users 

{:controller=>“users”, :action=>“index”}
formatted_users GET /users.:format
{:controller=>“users”, :action=>“index”}
POST /users
{:controller=>“users”, :action=>“create”}
POST /users.:format
{:controller=>“users”, :action=>“create”}
new_user GET /users/new
{:controller=>“users”, :action=>“new”}
formatted_new_user GET /users/new.:format
{:controller=>“users”, :action=>“new”}
edit_user GET /users/:id/edit
{:controller=>“users”, :action=>“edit”}
formatted_edit_user GET /users/:id/edit.:format
{:controller=>“users”, :action=>“edit”}
user GET /users/:id
{:controller=>“users”, :action=>“show”}
formatted_user GET /users/:id.:format
{:controller=>“users”, :action=>“show”}
PUT /users/:id
{:controller=>“users”, :action=>“update”}
PUT /users/:id.:format
{:controller=>“users”, :action=>“update”}
DELETE /users/:id
{:controller=>“users”, :action=>“destroy”}
DELETE /users/:id.:format
{:controller=>“users”, :action=>“destroy”}
root /
{:controller=>“users”, :action=>“index”}
/:controller/:action/:id
/:controller/:action/:id.:format

ive removed", “_method”=>“put”, “action”=>“index”,

Looks like a routing issue to me, should be calling the update action…
you could try explicitly calling update and see if it works…
<% form_for :user, @user, :url => { :action => “update” } do |f| %>

Did this problem start when you put the form in the partial?

You could try this:

view/users/edit.html.erb

<%= error_messages_for :user %>

<% form_for(@user) do |f| %>

<%= render :partial => ‘deliverable_form’, :locals => { :f => f } %>

<%# if you’re using 2.1 there is a nicer way:
http://workswithruby.com/2008/4/5-rails-tips
%>

<%= f.submit "Save" %>

put this in view/users/_form.html.erb

Personal Details

ImageName
<%= f.text_field :ImageName %>
Surname
<%= f.text_field :SurName %>
Forename
<%= f.text_field :ForeName %>
Title
<%= f.text_field :Title %>
Room No
<%= f.text_field :RoomNo %>
Phone No
<%= f.text_field :PhoneNo %>
EMail
<%= f.text_field :EMail %>

You can also check the html that is being rendered for the edit action
and make sure that the form has the right action.

You on FastCGI?

hostingrails.com has this problem when you’re on FastCGI and REST.

On Wed, Jun 4, 2008 at 6:57 PM, Dale C.
[email protected] wrote:

GET /users

:location => @user }
def update
:unprocessable_entity }
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end

Posted via http://www.ruby-forum.com/.


Ramon T.

Really?

I have 4 RESTful sites deployed to hostingrails.com all running on
FastCGI and haven’t had a single issue.

try sticking with <% form_for(@user) do |f| %>

John Y. wrote:

ive removed", “_method”=>“put”, “action”=>“index”,

Looks like a routing issue to me, should be calling the update action…
you could try explicitly calling update and see if it works…
<% form_for :user, @user, :url => { :action => “update” } do |f| %>

Ok I moved the form back into the edit and new html.erb files

and then added replaced <% form_for(@user) do |f| %> with <% form_for
:user, @user, :url => { :action => “update” } do |f| %> in the the
edit.html.erb file

On clicking the save button I get redirected to the url

http://www.website.com/users/users/1221

and get the following error

Unknown action

No action responded to 1221

I guess I need to do something with routing for the edit action.

I was trying to stick with just using

ActionController::Routing::Routes.draw do |map|
map.resources :users
map.root :controller => “users”
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
end