Rails 4.1中包含路由的表单

时间:2015-02-21 22:37:05

标签: ruby-on-rails ruby ajax ruby-on-rails-4.1 ruby-2.1.3

Rails 4.1,让驱动程序知道编辑并再次使用_form.html.erb。我试着做以下事情:

<% = Form_for @user, :controller => "admin/users" do | f |%>


<button type='submit'>Guardar</button>

<% end %>

#Controller   ===============

def new
    @user = User.new
  end




def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
          format.html { redirect_to admin_users_url, notice: "User #@user.name} was successfully created." }
          format.json { render :controller => 'admin/users', action: 'show', status: :created, location: @user }
      else
          format.html { render :controller => 'admin/users', action: 'new' }
          format.json { render json: @user.errors, status: :unprocessable_entity }
      end 
    end   
  end

但它不起作用我无法进入我的控制器路径(“admin / users”),如果有人可以帮助我

1 个答案:

答案 0 :(得分:0)

我假设您在config/routes.rb中定义了管理路由,如下所示:

# in config/routes.rb
namespace :admin do
  resources :users
end

请参阅:Rails Guide about namespaces in routes

然后,这应该适用于视图中的管理员命名空间表单:

<%= form_for [:admin, @user] do |f| %>
  ...
<% end %>

请参阅:Rails Guide about namespaces and form helper

您的控制器代码看起来没问题,但请确保控制器也是命名空间:

# in app/controllers/admin/users_controller.rb
module Admin
  class UsersController
    # ...
  end
end