什么会阻止对象被保存?

时间:2014-07-03 02:58:05

标签: ruby-on-rails object

在我的代码中我有这个:

def confirm
  @pal = Pal.find(params[:palid])
  @newuser = User.new(newuser_params)
  @newuser.username = @pal.phone

  if @newuser.save
    redirect_to(:controller => 'access', :action => 'index')
  else
    flash[:error] = "Retry."
    render template: 'pals/new_password_asign'
  end
end  

private

def newuser_params
  params.require(:newuser).permit(:username, :password_digest, :password_stored)
end

在提交我的注册表单后,我被重定向并出现错误"重试"我已经指定了。

知道我创建用户并通过rails console完全保存它们。

这让我想到什么可能阻止它被保存? 保存期间可能会出现什么问题?

=============================================== =======

Rails v4

Ruby v2.02

我正在使用Bcrypt-ruby进行身份验证。

=============================================== =======

更新#1

=============================================== ======

在添加建议的@mandeep解决方案之后,我现在得到了错误解释,在这个解释中我得到了

" 密码不能为空"

尽管在提交之前我确实写了密码

检查我的注册表单

<%= form_for(:newuser, :url => {:action => 'confirm', :palid => @pal.id}) do |f| %>
                    <% if @newuser.errors.any? %>
                      <div id="error_explanation">
                        <h2><%= pluralize(@newuser.errors.count, "error") %> prohibited this post from being saved:</h2>

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

        <table summary="user form fields">
                <tr>
                    <th><%= f.label(:username, "User Name") %></th> 
                    <td><%= f.text_field :username, :value => @newuser.username, :class => "controls", :disabled => "true" %></td>
                </tr>

                <tr>
                    <th><%= f.label(:password, "password") %></th>
                    <td><%= f.password_field(:password) %></td>
                </tr>

        </table>

                    <div class="form-actions">
    <%= f.submit nil, :class => 'btn btn-primary' %>
                      </div>

                <% end %>
                <% @newuser.password_stored = @newuser.password %>
            </div>
                </body>

============================================== < / p>

更新#2

@mandeep

请求后记录和参数
Started GET "/pals" for 127.0.0.1 at 2014-07-03 16:15:44 +0000
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by palsController#index as HTML
  pal Load (0.4ms)  SELECT `pals`.* FROM `pals`
  Rendered pals/index.html.erb within layouts/application (9.8ms)
Completed 200 OK in 280ms (Views: 260.9ms | ActiveRecord: 0.4ms)


Started GET "/pals" for 127.0.0.1 at 2014-07-03 16:15:45 +0000
Processing by palsController#index as HTML
  pal Load (0.7ms)  SELECT `pals`.* FROM `pals`
  Rendered pals/index.html.erb within layouts/application (6.3ms)
Completed 200 OK in 103ms (Views: 101.4ms | ActiveRecord: 0.7ms)


Started GET "/assets/access.css?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/bootstrap_and_overrides.css?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/pals.css?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/twitter/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:45 +0000


Started GET "/assets/access.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:46 +0000


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:46 +0000


Started GET "/assets/pals.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:46 +0000


Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:46 +0000


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-07-03 16:15:46 +0000


Started GET "/pals/new" for 127.0.0.1 at 2014-07-03 16:16:11 +0000
Processing by PalsController#new as HTML
  Rendered pals/_form.html.erb (24.7ms)
  Rendered pals/new.html.erb within layouts/application (43.6ms)
Completed 200 OK in 106ms (Views: 101.2ms | ActiveRecord: 1.6ms)


Started POST "/pals" for 127.0.0.1 at 2014-07-03 16:16:19 +0000
Processing by PalsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8X6LD0g6E1HybdAF/FOlM09iuQLWExBY8dY5CqSDZsM=", "pal"=>{"owner"=>"John", "title"=>"", "phone"=>"22", "price"=>"", "logtype"=>"", "description"=>"", "address"=>"", "latitude"=>"", "longitude"=>""}, "commit"=>"Create pal"}
   (0.3ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `pals` (`address`, `created_at`, `description`, `logtype`, `owner`, `phone`, `title`, `updated_at`) VALUES ('', '2014-07-03 16:16:19', '', '', 'John', '22', '', '2014-07-03 16:16:19')
   (56.6ms)  COMMIT
  User Load (0.1ms)  SELECT `users`.* FROM `users`  WHERE `users`.`username` = '22'
Redirected to http://localhost:3000/pals/confirm?palid=5
Completed 302 Found in 77ms (ActiveRecord: 58.1ms)


Started GET "/pals/confirm?palid=5" for 127.0.0.1 at 2014-07-03 16:16:19 +0000
Processing by PalsController#confirm as HTML
  Parameters: {"palid"=>"5"}
  pal Load (0.6ms)  SELECT  `pals`.* FROM `pals`  WHERE `pals`.`id` = 5 LIMIT 1
  Rendered pals/confirm.html.erb within layouts/application (6.4ms)
Completed 200 OK in 63ms (Views: 52.4ms | ActiveRecord: 0.8ms)


Started GET "/assets/access.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/pals.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/bootstrap_and_overrides.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/twitter/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/access.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/pals.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:19 +0000


Started POST "/pals/confirm?palid=5" for 127.0.0.1 at 2014-07-03 16:16:29 +0000
Processing by PalsController#confirm as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8X6LD0g6E1HybdAF/FOlM09iuQLWExBY8dY5CqSDZsM=", "newuser"=>{"password"=>"[FILTERED]"}, "commit"=>"Save Newuser", "palid"=>"5"}
  pal Load (0.2ms)  SELECT  `pals`.* FROM `pals`  WHERE `pals`.`id` = 5 LIMIT 1
   (0.6ms)  BEGIN
   (0.1ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
  Rendered pals/confirm.html.erb within layouts/application (1.8ms)
Completed 200 OK in 60ms (Views: 50.6ms | ActiveRecord: 1.1ms)


Started GET "/assets/access.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/bootstrap_and_overrides.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/pals.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:29 +0000


Started GET "/assets/twitter/bootstrap/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/twitter/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/pals.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/access.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-07-03 16:16:30 +0000

2 个答案:

答案 0 :(得分:1)

如果你看一下你的代码

def confirm
  pal = Pal.find(params[:palid])  # you have find your pal and store it in "pal"
  @newuser = User.new(newuser_params)
  @newuser.username = @pal.phone  # you are using instance variable "@pal" which is not defined
  # your other logic
end

相反,您应该拥有以下代码:

def confirm
  @pal = Pal.find(params[:palid])  
  @newuser = User.new(newuser_params)
  @newuser.username = @pal.phone
  # your other logic
end 

同样在您的表单中,您应该输入此代码

<% if @newuser.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@newuser.errors.count, "error") %> prohibited this post from being saved:</h2>

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

如果表单中有验证错误,则会显示。有关详细信息,请参阅here

<强>更新

如果您查看从表单传递的参数,请注意

Parameters: {"utf8"=>"✓", "authenticity_token"=>"8X6LD0g6E1HybdAF/FOlM09iuQLWExBY8dY5CqSDZsM=", "newuser"=>{"password"=>"[FILTERED]"}, "commit"=>"Save Newuser", "palid"=>"5"}

您的表单中有几个错误:

一个。要么您没有在表单中输入用户名,要么您还没有正确填写表单,因为在params中它只显示密码字段 "newuser"=>{"password"=>"[FILTERED]"}

湾您尚未准许您的密码字段,而不是允许您允许的密码: password_digest,:password_stored 。您的newuser_params应如下所示:

def newuser_params
  params.require(:newuser).permit(:username, :password)
end

℃。什么是"palid"=>"5"在你的参数中做什么?它是您表单中的字段还是因为它与newuser params

无关

答案 1 :(得分:0)

检查@newuser中的验证错误,即@newuser.errors。有一些方法可以跳过验证。也许你在控制台中使用它们。

相关问题