0

我在将类 Group 的对象实例保存到数据库时遇到问题

请帮助我了解问题出在哪里

这是模型

class User < ActiveRecord::Base
  has_many :groups
end

class Group < ActiveRecord::Base
  belongs_to :user
  has_many :people
end

和路线.rb

resources :users do |user|
  resources :groups do |group| 
    resources :people 
  end
end

我使用 Devise 构建用户模型和认证系统

用例:用户注册,现在他/她可以创建组。如果用户未注册和登录,则不允许创建组。这部分有效。

然后用户转到 new_user_group_path,填写表格并单击创建。此时页面刷新,我看到相同的表单(空)和 url,其中包含在 previos 步骤中填充表单的所有参数。像这样users/user_id/groups/new?utf8=✓&amp;authenticity_token=...

同时列表user_groups_path不显示任何新对象

这是 Group 的控制器

class GroupsController < ApplicationController
  respond_to :html, :xml, :json

  before_filter :authenticate_user!

  def new
    @user = current_user
    @group = @user.groups.build(:user_id => current_user.id,
      :location=>params[:group][:location])
    respond_with(@group)
  end

  def create
    @user = current_user
    @group = @user.groups.build(:user_id => current_user.id,
      :location=>params[:group][:location])
    if @group.save
      redirect_to user_groups_path(current_user)
    else
      render 'new'
    end
  end

这是表格

<%= form_for @group, :url => user_groups_path(:user_id => current_user.id) do |f| %>
  <% if @group.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@group.errors.count, "error") %> prohibited this group from being saved:</h2>

    <ul>
      <% @group.errors.full_messages.each do |msg| %>
         <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
  <% end %>

<%= f.label :location %>
<%=f.text_field :location %>

<etc...>
<% end %>

UPD 的结果rake routes

new_user_session GET    /users/sign_in(.:format)                                               devise/sessions#new
            user_session POST   /users/sign_in(.:format)                                               devise/sessions#create
    destroy_user_session GET    /users/sign_out(.:format)                                              devise/sessions#destroy
           user_password POST   /users/password(.:format)                                              devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                                          devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                                         devise/passwords#edit
                         PUT    /users/password(.:format)                                              devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                                                devise/registrations#cancel
       user_registration POST   /users(.:format)                                                       devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                                               devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                                                  devise/registrations#edit
                         PUT    /users(.:format)                                                       devise/registrations#update
                         DELETE /users(.:format)                                                       devise/registrations#destroy

       user_groups GET    /users/:user_id/groups(.:format)                                 groups#index
                         POST   /users/:user_id/groups(.:format)                                 groups#create
    new_user_group GET    /users/:user_id/groups/new(.:format)                             groups#new
   edit_user_group GET    /users/:user_id/groups/:id/edit(.:format)                        groups#edit
        user_group GET    /users/:user_id/groups/:id(.:format)                             groups#show
                         PUT    /users/:user_id/groups/:id(.:format)                             groups#update
                         DELETE /users/:user_id/groups/:id(.:format)                             groups#destroy
                   users GET    /users(.:format)                                                       users#index
                         POST   /users(.:format)                                                       users#create
                new_user GET    /users/new(.:format)                                                   users#new
               edit_user GET    /users/:id/edit(.:format)                                              users#edit
                    user GET    /users/:id(.:format)                                                   users#show
                         PUT    /users/:id(.:format)                                                   users#update
                         DELETE /users/:id(.:format)                                                   users#destroy

数据库是 sqlite3

我哪里错了?

感谢任何帮助

UPD

/users/:user_id/groups/new这是在页面上生成的 HTML :

<!DOCTYPE html>
<html>
<head>
<title>PR0124</title>
<link href="/assets/bootstrap.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/javascripts/bootstrap.min.js" type="text/javascript"></script>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/js/jquery.min.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/js/jquery.stepy.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/js/jquery.stepy.min.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/js/jquery.validate.min.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/test/lib/jasmine-html.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/test/lib/jasmine-jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/test/lib/jasmine.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.stepy/test/spec.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap.min.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="/l/wVZh+Tr/+WZbn5l5oJ7wQY1mU8kj0vyA61q1Bghw=" name="csrf-token" />
</head>
<body>
<div class='container'>
<form id='newgroup'>
  <h1>Create a new group</h1>
   <form accept-charset="UTF-8" action="/users/39/groups" class="new_group" id="new_group" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="/l/wVZh+Tr/+WZbn5l5oJ7wQY1mU8kj0vyA61q1Bghw=" /></div>
   <fieldset id=”1”&gt;
   <div class="field">
     <label id=location>Location</label>
       <input class="input-xlarge" id="group_location" name="group[location]" placeholder="Type here" required="required" size="30" type="text" value="mnjsjksjkl" />
   </div>
   </fieldset>
   <fieldset id=”2”&gt;
   <div class="field">
     <label id='name'>Name</label>
       <input class="input-xlarge" id="group_name" name="group[name]" placeholder="Type here" required="required" size="30" type="text" value="hjhjggh" />
   </div>
   </fieldset>
   <fieldset id=”3”&gt;
   <input name="group[leader]" type="hidden" value="0" /><input id="group_leader" name="group[leader]" style="float:left; margin-right:9px" type="checkbox" value="1" />
   <label for="group_leader">has a leader</label><br />
   </fieldset>
   <!-- Action button -->
   <div class="actions">
       <input class="btn btn-large" name="commit" type="submit" value="Create a Group" />
   </div>
   </form>
  </form>
 </div>
<script type="text/javascript">
$('#newgroup').stepy();
</script>
   </body>
   </html>

这是 development.log

Started GET "/users/39/group/new?utf8=%E2%9C%93&authenticity_token=%2Fl%2FwVZh%2BTr%2F%2BWZbn5l5oJ7wQY1mU8kj0vyA61q1Bghw%3D&group%5Blocation%5D=mnjsjksjkl&group%5Bname%5D=hjhjggh&group%5Bleader%5D=0&commit=has+a+leader" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Processing by GroupsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"/l/wVZh+Tr/+WZbn5l5oJ7wQY1mU8kj0vyA61q1Bghw=", "group"=>{"location"=>"mnjsjksjkl", "name"=>"hjhjggh", "leader"=>"0"}, "commit"=>"Create a group", "user_id"=>"39"}
  [1m[35mUser Load (0.3ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 39 LIMIT 1
  Rendered groups/_form.html.erb (2.2ms)
  Rendered groups/new.html.erb within layouts/vertical (3.0ms)
Completed 200 OK in 32ms (Views: 29.2ms | ActiveRecord: 0.3ms)


Started GET "/assets/bootstrap.css?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /bootstrap.css - 304 Not Modified (0ms)


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/javascripts/bootstrap.min.js" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /javascripts/bootstrap.min.js - 404 Not Found (3ms)

ActionController::RoutingError (No route matches [GET]         "/assets/javascripts/bootstrap.min.js"):
  actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.12) lib/rails/engine.rb:479:in `call'
  railties (3.2.12) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)


Started GET "/assets/bootstrap.min.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /bootstrap.min.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/js/jquery.min.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/js/jquery.min.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/js/jquery.stepy.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/js/jquery.stepy.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/js/jquery.stepy.min.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/js/jquery.stepy.min.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/js/jquery.validate.min.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/js/jquery.validate.min.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/test/lib/jasmine-html.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/test/lib/jasmine-html.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/test/lib/jasmine-jquery.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:30 +0400
Served asset /jquery.stepy/test/lib/jasmine-jquery.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/test/lib/jasmine.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:31 +0400
Served asset /jquery.stepy/test/lib/jasmine.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.stepy/test/spec.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:31 +0400
Served asset /jquery.stepy/test/spec.js - 304 Not Modified (0ms)


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-08-22 03:03:31 +0400
Served asset /jquery.js - 304 Not Modified (0ms)`
4

1 回答 1

0

然后用户转到 new_user_group_path,填写表格并单击创建。此时页面刷新,我看到相同的表单(空)和 url,其中包含在 previos 步骤中填充表单的所有参数。像这样users/user_id/groups/new?utf8=✓&amp;authenticity_token=...

这让我大吃一惊——表单应该作为 POST 提交,但这是一个 GET。您能否发布从/users/:user_id/groups/new页面生成的 HTML 以及log/development.log提交表单时显示的内容?

于 2013-08-21T13:00:41.590 回答