开发者

Form behavior posting nested resource

I am using Ruby on Rails 3 and I would like to change its conventional behavior on posting a form in order to post from a signup action to the crea开发者_如何学运维te action instead that from the new action. That is, I would like to use the signup action instead of the (conventional) new action in my User controller and trigger the create action to save my model data that contains nested resources.

In my /config/routes.rb file I have:

resources :users do
  collection do
    get  'signup'
  end

  resource :profile
end

In my /app/controllers/users_controller.rb I have

class UsersController < ApplicationController
  def signup
    @signup_user = User.new(params[:user])
    @signup_user.build_profile # NOTE: Nested resource
    ...
  end

  def create
    ...
    @signup_user.save

    respond_to do |format|
      format.html { render :action => :signup } # signup.html.erb
end
  end
end

In my /app/views/users/signup.html.erb file I have

<%= form_for @signup_user do |f| %>
  ...
<% end %>

My problem is that if I submit the above form, I will be redirected to the index action of the user controller and not to the create action I expect. It seams that the form posts only to the index action.

How can I solve the problem?


I tryed to use the following

<%= form_for( :user, @signup_user, :url => { :controller => "users", :action => "create" }, :html => { :method => :post } do |f| %>

but I have still the problem: I am redirected to the index action.


SOLUTION

The problem did seam to be in the routers.rb. The correct code was

resources :users do
  collection do
    get  'signup'
    post 'create'
  end

  resource :profile
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜