我的模型中有一个数组字段,我正在尝试更新它.
我的强参数方法如下
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
是一个数组的事实有关,因为其他一切看起来都很好.然后,我可能是错的.那么,我的代码出了什么问题,为什么在明确允许的情况下不让我保存类别字段呢?谢谢.
通过关于控制器测试的教程,作者给出了测试控制器动作的rspec测试的示例.我的问题是,为什么他们使用的方法attributes_for
了build
?attributes_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
我试图围绕使用initialize方法的目的.在Hartl的教程中,他使用了这个例子.
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
Run Code Online (Sandbox Code Playgroud)
是初始化设置实例变量@name
和@email
属性,如果是,为什么我们有参数attributes = {}
?
我将我的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
简单的问题.我有一个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) 我正在尝试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
.我究竟做错了什么?
我有一个表单,在表单中有一个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=>"Enter search
term..."}" />
Run Code Online (Sandbox Code Playgroud)
为什么这样做以及如何修复它以使占位符有效?
我问过这个问题,但我提出的具体问题发生了巨大变化.
我有一段代码:
<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-controller
if 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)
怎么了,怎么解决这个问题?
我刚刚开始玩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) 我遇到了一个特殊情况,即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
attributes ×2
javascript ×2
ruby ×2
angularjs ×1
css ×1
ecmascript-6 ×1
factory-bot ×1
html ×1
jquery ×1
json ×1
login ×1
placeholder ×1
postgresql ×1
rspec ×1
sass ×1
sinatra ×1
this ×1