小编jas*_*328的帖子

Rails 4数组未允许的参数

我的模型中有一个数组字段,我正在尝试更新它.

我的强参数方法如下

def post_params
  params["post"]["categories"] = params["post"]["categories"].split(",")

  params.require(:post).permit(:name, :email, :categories)
end
Run Code Online (Sandbox Code Playgroud)

我在控制器中的动作如下

def update
  post = Post.find(params[:id]

  if post and post.update_attributes(post_params)
    redirect_to root_url
  else
    redirect_to posts_url
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,每当我提交更新帖子时,在我的开发日志中我都会看到

Unpermitted parameters: categories
Run Code Online (Sandbox Code Playgroud)

传递的参数是

  Parameters: {"utf8"=>"?", "authenticity_token"=>"auth token", "id"=>"10", 

"post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}}
Run Code Online (Sandbox Code Playgroud)

我想这与属性categories是一个数组的事实有关,因为其他一切看起来都很好.然后,我可能是错的.那么,我的代码出了什么问题,为什么在明确允许的情况下不让我保存类别字段呢?谢谢.

ruby ruby-on-rails strong-parameters ruby-on-rails-4

52
推荐指数
3
解决办法
3万
查看次数

FactoryGirl和Rspec测试中attributes_for的含义

通过关于控制器测试的教程,作者给出了测试控制器动作的rspec测试的示例.我的问题是,为什么他们使用的方法attributes_forbuildattributes_for除了它返回值的散列之外,没有明确的解释.

it "redirects to the home page upon save" do
  post :create, contact: Factory.attributes_for(:contact)
  response.should redirect_to root_url
end
Run Code Online (Sandbox Code Playgroud)

可在此处找到教程链接:http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html该示例位于开头主题部分Controller testing basics

attributes rspec ruby-on-rails ruby-on-rails-3 factory-bot

33
推荐指数
1
解决办法
2万
查看次数

rails中的initialize方法有什么作用

我试图围绕使用initialize方法的目的.在Hartl的教程中,他使用了这个例子.

def initialize(attributes = {})
   @name = attributes[:name]
   @email = attributes[:email]
end
Run Code Online (Sandbox Code Playgroud)

是初始化设置实例变量@name@email属性,如果是,为什么我们有参数attributes = {}

attributes ruby-on-rails ruby-on-rails-3

27
推荐指数
1
解决办法
3万
查看次数

没有这样的文件或目录 - getcwd

我将我的Sinatra应用程序上传到了Beanstalk.当我访问我的网站时,我的日志将被退回

No such file or directory - getcwd
Run Code Online (Sandbox Code Playgroud)

该应用程序之前正在工作.我认为这个问题与我将SASS添加到我的应用程序这一事实有关,但我并不积极.在我config.ru,我有以下代码处理SASS ...

# use scss for stylesheets
Sass::Plugin.options[:style] = :compressed
use Sass::Plugin::Rack
Run Code Online (Sandbox Code Playgroud)

如果它可能是另一个问题,请告诉我,我可以提供更多信息.谢谢.

ruby sass sinatra amazon-web-services amazon-elastic-beanstalk

23
推荐指数
3
解决办法
2万
查看次数

在jQuery Selector中连接

简单的问题.我有一个js变量我想在jQuery选择器中连接.但它不起作用.也没有出现任何错误.怎么了?如何在jQuery选择器中正确地将变量连接到某些文本.

<div id="part2"></div>

<script type="text/javascript" >

$('#button').click(function () 
{
var text = $('#text').val();    
var number = 2;
$.post('ajaxskeleton.php', {
red: text       
}, 
    function(){
    $('#part' + number).html(text);
    }); 
});


</script> 
Run Code Online (Sandbox Code Playgroud)

jquery concatenation

22
推荐指数
1
解决办法
11万
查看次数

箭头功能和此

我正在尝试ES6,并希望在我的函数中包含一个属性,就像这样

var person = {
  name: "jason",

  shout: () => console.log("my name is ", this.name)
}

person.shout() // Should print out my name is jason
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此代码控制台时只记录日志my name is.我究竟做错了什么?

javascript this ecmascript-6 arrow-functions

19
推荐指数
3
解决办法
8492
查看次数

文本字段标记占位符返回为值

我有一个表单,在表单中有一个text_field_tag.它写成这个......

<%= text_field_tag "search",  :placeholder => 'Enter search term...' %>
Run Code Online (Sandbox Code Playgroud)

但是,当生成HTML时,这将返回页面源...

<input id="search" name="search" type="text" value="{:placeholder=&gt;&quot;Enter search 
term...&quot;}" />
Run Code Online (Sandbox Code Playgroud)

为什么这样做以及如何修复它以使占位符有效?

html css ruby-on-rails placeholder ruby-on-rails-3

18
推荐指数
1
解决办法
1万
查看次数

如何基于条件实例化ng-controller

我问过这个问题,但我提出的具体问题发生了巨大变化.

我有一段代码:

  <div ng-attr-controller="{{pings || 'PingsCtrl as pings' }}">
    <h1 ng-click="pings.press()">asdf</h1>
  </div>
Run Code Online (Sandbox Code Playgroud)

此代码注入两个html页面.一页已经调用PingsCtrl.另一个没有.我真的想保持这段代码DRY,我只想要一个上面的代码参考.

我如何编写上面的代码来生成ng-controllerif PingsCtrl尚未实例化.

这是两个html页面.

HTML

// First page
<html ng-app="coolApp">
  <div ng-controller="PingsCtrl as pings">
    <div ng-attr-controller="{{pings || 'PingsCtrl as pings' }}">
      <h1 ng-click="pings.press()">asdf</h1>
    </div>
  </div>
</html>

// Second page
<html ng-app="coolApp">
  <div ng-attr-controller="{{pings || 'PingsCtrl as pings' }}">
    <h1 ng-click="pings.press()">asdf</h1>
  </div>
</html>
Run Code Online (Sandbox Code Playgroud)

Javascript在这里:

angular.module('coolApp', [])

.controller('PingsCtrl', function() {
  var vm = this;

  vm.press = function() {alert(123)};
})
Run Code Online (Sandbox Code Playgroud)

怎么了,怎么解决这个问题?

javascript angularjs ng-controller

17
推荐指数
1
解决办法
1406
查看次数

PostgreSQL上的现有用户登录失败

我刚刚开始玩PostgreSQL.我的目标是在postgres之外创建一个具有所有相同权限的新用户,并为我的ror应用程序创建一个数据库.我可以在postgres下登录.我确实创建了一个名为的用户Jason,这很好,但是当我这样做时,sudo -u username psql我收到以下错误...

sudo: unknown user: Jason
sudo: unable to initialize policy plugin
Run Code Online (Sandbox Code Playgroud)

我可以通过检查postgres控制台中的\ dg来确定用户名是否存在.

                         List of roles
Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
Jason     | Superuser, Create role, Create DB, Replication | {}
postgres  | Superuser, Create role, Create DB, Replication | {}
Run Code Online (Sandbox Code Playgroud)

是什么导致了这个问题?此外,我检查了我的本地,pg_hba.conf并有我认为是正确的设置.

# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres …
Run Code Online (Sandbox Code Playgroud)

postgresql login

16
推荐指数
1
解决办法
9477
查看次数

主动模型序列化器 - 提高渲染性能

我遇到了一个特殊情况,即ActiveModel :: Serializer生成的渲染json非常慢(大约6-8秒).如何提高渲染速度?这是代码.

楷模:

class Comment < ActiveRecord::Base
  has_many :children_comments,
           class_name: 'Comment',
           foreign_key: 'parent_comment_id'

  belongs_to :user
  belongs_to :parent_comment,
             class_name: 'Comment',
             foreign_key: 'parent_comment_id'
end
Run Code Online (Sandbox Code Playgroud)

串行器:

class CommentSerializer < ActiveModel::Serializer
  include ActionView::Helpers::DateHelper

  attributes :id, :message, :created_at_in_words,
             :created_at, :parent_comment_id
  belongs_to :user
  has_many :children_comments

  def created_at_in_words
    time_ago_in_words(object.created_at) + ' ago'
  end

  def children_comments
    object.children_comments.map do |comment|
      CommentSerializer.new(comment).as_json
    end
  end
end

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :avatar_url

  def avatar_url
    object.avatar.url
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的控制器中我有

  parent_comments = Comment.where(parent_comment_id: nil)

  render status: :ok,
         json: parent_comments,
         each_serializer: …
Run Code Online (Sandbox Code Playgroud)

serialization json ruby-on-rails active-model-serializers ruby-on-rails-4

16
推荐指数
2
解决办法
1810
查看次数