开发者

update instead of show being called in REST rails form submission

开发者 https://www.devze.com 2023-03-14 21:46 出处:网络
I have a form that a user submits. The form is for a user.I have the following in my routes.rb file: resources :users, :except => [:new]

I have a form that a user submits. The form is for a user. I have the following in my routes.rb file:

 resources :users, :except => [:new]

  root :to => 'pages#home'
  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'

In the users_controller.rb:

  def create
    @title = "Home"
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        format.html { redirect_to user_path(@user), :flash => {:success => "Your friend's details have been saved.The surprise has begin"} }
        format.js
      else
        format.html { render :template => 'pages/home'}
        format.js
      end
    end
  end

This form is being submitted with a regular POST request and not Ajax.

I want to know why the Update template is being requested after the form is successfully submitted instead of the Show.

The output from rake routes is as follows:

    users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
          POST   /users(.:format)          {:action=>"create", :controller=>"users"}
edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
     user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
          PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
          DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}
     root        /(.:format)               {:controller=>"开发者_开发百科pages", :action=>"home"}
  contact        /contact(.:format)        {:controller=>"pages", :action=>"contact"}
    about        /about(.:format)          {:controller=>"pages", :action=>"about"}
     help        /help(.:format)           {:controller=>"pages", :action=>"help"}

Thanks in advance.


What is the code for the controller and view of this form? Is there an existing user object when you generate the form or do you create a new user object (ie. @user = User.new in your controller)? If there is an existing user then the form will submit to update.


Your route and controller code look good, i.e. it should be sending you to the show method in your users_controller. I'd put some debbugging code in there, i.e. logger.debug("Inside Show") to see if the show method was being called.

You could also include the output of rake routes in your edited question, it might be informative to show where the user_path helper is going to send the redirect.

ian.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号