Tag: 强参数

Ruby on Rails:无法实现星级评分系统

我正在尝试为我的应用添加一个简单的星级评分系统,并以此教程为例。 我有用户,酒店和评级模型。 依赖关系是: (rating.rb) belongs_to :user belongs_to :hotel (hotel.rb)&(user.rb) has_many :ratings 并在酒店视图中使用以下代码我收到此错误: 酒店#show中的NameError 类的未定义局部变量或方法`user’… (在<%= form_for …行中) 酒店景观 (show.html.erb): {:id => form_id, :class => “star_rating_form”} do |f| %> @hotel.id %> current_user.id %> form_id + “_stars” %> <li class="rating_star" id="_” data-stars=”” data-form-id=””> 评级控制器是: def create end def update end def rating_params params.require(:rating).permit(:stars) end 迁移文件是: create_table :ratings […]

ActionController :: ParameterMissing param丢失或值为空

我无法解决这个问题。 当我尝试使用“Chatroom #new”方法时,我得到了这个错误, ActionController :: ParameterMissing param丢失或值为空 。 下面的代码是ChatroomController。 class ChatroomsController :id,:user_id,:messages} end #before_aciton def find_room_owner @item = Item.find(params[:item_id]) @buyer = User.find(params[:user_id]) @seller = Product.find_by(user_id:@item.user_id) end def objects_for_index @user = User.find(params[:user_id]) @items = Item.all end end 下面的代码是Message#new的视图。 Chatroom#new Find me in app/views/chatroom/new.html.erb 下面的代码是Message的迁移。 class CreateMessages < ActiveRecord::Migration def change create_table :messages do |t| t.integer :user_id […]

has_one嵌套属性不保存

我有两个模型Project和ProjectPipeline。 我想创建一个Project表单,该表单也包含ProjectPipeline模型中的字段。 我已成功创建表单但是当我点击保存时,值不会存储在数据库中。 project.rb class Project < ActiveRecord::Base has_one :project_pipeline accepts_nested_attributes_for :project_pipeline self.primary_key = :project_id end projectpipeline.rb class ProjectPipeline < ActiveRecord::Base belongs_to :project, autosave: :true validates_uniqueness_of :project_id end 我并不总是想要一个项目管道,但是在适当的条件下,基于用户查看项目。 我希望构建项目管道字段,但只有在用户选择保存/填充它们时才会保存。 因此,当项目显示时,我使用project_id构建一个临时项目管道:来自params [:id](不确定我是否真的需要这样做)。 然后,当项目保存时,我使用create_attributes。 但是如果它已经被创建或构建,我只想让has_one和belongs_to关联启动,然后使用update_attributes。 我的问题是当我试图保存时,如果我使用params [:project_pipeline]或者如果我使用project_params则根本没有保存,我要么遇到’Forbidden Attribute’错误。 我已经检查并重新检查了我在project_params中的所有字段,甚至尝试使用project_pipeline_params,但感觉不对。 这让我疯了,我需要睡觉。 projects_controller.rb def show @project = Project.find(params[:id]) if @project.project_pipeline else @project.build_project_pipeline(project_id: params[:id]) end autopopulate end def update […]

Rails:将嵌套属性与strong_params合并

在Rails 4中,可以将额外参数与用户生成的参数合并,如下所示: params.require(:post).permit([:title, :body]).merge(user: current_user) 也可以包含嵌套属性,如下所示: params.require(:post).permit([:title, :body, sections_attributes: [:title, :section_type]]) 现在,如果我想将额外参数合并到嵌套模型中,该怎么办? 我试过这个: params.require(:post).permit([:title, :body, sections_attributes: [:title, :section_type]]).merge(user: current_user, sections_attributes: [user: current_user]) 但是当我之后使用调试器检查params时,我发现user已经覆盖了其他section_attributes而不是与它们合并。 有没有更好的方法来解决这个问题? Full backtrace ————– – activemodel (4.0.0.rc1) lib/active_model/attribute_methods.rb:436:in `method_missing’ – activerecord (4.0.0.rc1) lib/active_record/attribute_methods.rb:131:in `method_missing’ – activerecord (4.0.0.rc1) lib/active_record/nested_attributes.rb:432:in `block in assign_nested_attributes_for_collection_association’ – activerecord (4.0.0.rc1) lib/active_record/nested_attributes.rb:431:in `assign_nested_attributes_for_collection_association’ – activerecord (4.0.0.rc1) lib/active_record/nested_attributes.rb:322:in `comments_attributes=’ – […]

强参数和多维数组

我正在使用Rails 3.2.6和强参数 gem。 所以,我有一个具有典型更新操作的控制器: # PUT /api/resources/:id def update @resource.update_attributes! permited_params respond_with_json @resource, action: :show end 然后,我有permited_params方法 def permited_params params.permit(:attr1, :attr2, :attr3) end 问题是:attr3是一个多维数组,如下所示: [[1, 2], [2, 5, 7]] :attr3 [[1, 2], [2, 5, 7]] 在文档之后,我需要指定:attr3作为数组。 但… params.permit(:attr1, :attr2, :attr3 => []) #inspecting permited_params: {“attr1″=>”blah”, “attr2″=>”blah”} params.permit(:attr1, :attr2, :attr3 => [[]]) #inspecting permited_params: {“attr1″=>”blah”, “attr2″=>”blah”, […]

Rails 4 Strong Params has_many with JSON

我试图在客户端传递json并让rails负责处理对象创建。 这是我的模特: class Order true belongs_to :menu_session end class OrderItem < ActiveRecord::Base belongs_to :order has_one :menu_item end 调节器 class OrderController [:menu_item_id]) end end json数据: {‘order’: {‘comments’: ‘none’, ‘menu_session_id’: ‘9’, ‘order_items’:[{‘menu_item_id’: ‘5’}, {‘menu_item_id’: ‘5’}]}}; 这个javascript var data = {}; data.order = {‘comments’: ‘none’, ‘menu_session_id’: ‘9’, ‘order_items’:[{‘menu_item_id’: ‘5’}, {‘menu_item_id’: ‘5’}]}; $.post(‘http://localhost:3000/order/create’, orders, function(){}, ‘json’); 最后,错误日志: Started POST […]

Strong_parameters无效

使用Ruby 1.9.3,Rails 3.2.13,Strong_parameters 0.2.1: 我已经遵循了教程和railscast中的所有指示,但我无法使strong_parameters正常工作。 它应该是非常简单的东西,但我看不出错误在哪里。 配置/初始化/ strong_parameters.rb: ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection) 配置/ application.rb中 config.active_record.whitelist_attributes = false 应用程序/模型/ product.rb class Product < ActiveRecord::Base end 应用程序/控制器/ products_controller.rb: class ExpedientesController < ApplicationController … def create @product = Product.new(params[:product]) if @product.save redirect_to @product else render :new end end end 这会像预期的那样引发Forbidden Attributesexception。 但是当我搬到: … def create @product = Product.new(product_params) # and […]

Rails 4 +设计使用电子邮件或用户名和强参数登录

我是RoR的新手并且坚持这个设计问题。 我想允许用户使用电子邮件或用户名登录(使用用户名注册已经可以)。 我遵循了这些文章: 第1条和第2条 ,您可以看到以下结果: application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) } end end user.rb class User [:login] validates_uniqueness_of :username validates_presence_of :username validates :username, length: { in: 4..20 } def self.find_first_by_auth_conditions(warden_conditions) conditions = […]

Rails 4设计强参数管理模型

我使用设计制作了用户和管理模型。 我在app / controllers / application_controller.rb文件中使用了强参数 class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) } end […]

由于* _attributes后缀的forms,强参数,嵌套属性和Mongoid似乎不起作用?

我正在尝试在我的主要Person模型上嵌入属性组,以帮助保持我的代码组织,但Rails 4的强大参数正在阻碍。 情景 我有一个Person模型和一个PersonName模型,如下所示: class Person include Mongoid::Document embeds_one :name, class_name: ‘PersonName’ accepts_nested_attributes_for :name end class PersonName include Mongoid::Document embedded_in :person, inverse_of: :name # fields here end 然后我在表单中使用fields_for帮助程序将PersonName属性嵌套在Person _form : 最后我允许控制器中的name属性,以便Strong Params允许嵌套属性通过: def person_params params.require(:person).permit(:name) end 问题 它不起作用,我得到错误Unpermitted parameters: name_attributes 。 发生的事情是当我调用f.fields_for :name … a“_attributes”get附加到HTML表单中的字段时。 例如,如果我有一个字段first_name那么表单如下所示: 代替: 据我所知,这是预期的行为,所以似乎.permit方法是事情无法工作的地方。 尝试过的工作 我已经尝试将Strong Params行更改为: params.require(:person).permit(:name_attributes) 但这不起作用。 使用permit! 确实有效,但我不喜欢它,因为据我所知它是一个完整的反模式。 […]