Tag: ruby on rails 5

添加自定义参数以设计注册 – 未允许的参数

我一直在尝试自定义设计寄存器方法以注册更多参数并更新更多(到目前为止没有运气),但我总是得到Unpermitted parameters:错误。 我尝试使用Devise和https://github.com/plataformatec/devise#strong-parameters 添加额外的注册字段 ,但我无法克服这一点。 我还考虑过创建一个新表来保存外键的用户ID并放入像user_id, display_name, profile_picture这样的东西,但是当我尝试从同一页面提交所有东西时我会遇到同样的问题(搞乱了设计控制器)。 您对我如何解决这个问题有什么建议吗? 我还有什么要发布的? 的routes.rb devise_for :users, controllers: { registrations: ‘users/registrations’ } 用户/ REGC def create build_resource(registration_params) if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location => after_sign_up_path_for(resource) else set_flash_message :notice, :”signed_up_but_#{resource.inactive_message}” if is_navigational_format? respond_with resource, :location => after_sign_up_path_for(resource) end else clean_up_passwords respond_with resource […]

路由会话#destroy action

我正在链接到Sessions控制器的destroy动作,如下所示: routes.rb中: resources :sessions, only: [:new, :create, :destroy] Rails抱怨上面的链接: 没有路由匹配{:action =>“destroy”,:controller =>“sessions”}缺少必需的密钥:[:id] 当没有对象ID提供链接时,如何链接到destroy操作并在Rails中保留REST /资源方法?

Rails 5 – 找不到生成器’rspec:install’

在我的rails 5.0.0应用程序中,我已将以下内容添加到我的Gemfile中: group :development, :test do gem ‘byebug’, platform: :mri gem ‘rspec-rails’, ‘~> 3.5’, ‘>= 3.5.2’ end 我运行了bundle install 。 然后gem成功安装。 然后我跑了以下: rails generate rspec:install 但我得到一个错误说: Running via Spring preloader in process 8893 Could not find generator ‘rspec:install’. Maybe you meant ‘css:assets’, ‘assets’ or ‘scaffold’ Run `rails generate –help` for more options. 在这个错误上发布了大量其他问题( Could not […]

在Rails 5 Application Record类中包含一个模块

我正在使用Application Record来简化整个应用程序中的共享逻辑。 这是一个为布尔值及其逆写入范围的示例。 这很好用: class ApplicationRecord { where(“#{attr}”: true) }) scope(opposite, -> { where(“#{attr}”: false) }) if opposite.present? end end class User < ApplicationRecord boolean_scope :verified, :unverified end class Message < ApplicationRecord boolean_scope :sent, :pending end 我的应用程序记录类得到了足够长的时间,我将它分解为单个模块并根据需要加载它们是有意义的。 这是我尝试的解决方案: class ApplicationRecord { where(“#{attr}”: true) }) scope(opposite, -> { where(“#{attr}”: false) }) if opposite.present? end end class […]

如何在Rails 5中期望RSpec中的Params哈希?

我正在升级到Rails 5,即使我传递了我应该的数据,它已经破坏了我的RSpec。 问题显然在这里: expected: ({“name”=>”MyString”}) got: (“MyString”} permitted: true>) 这意味着我需要能够修复我的控制器断言,以便它能够预期后者。 这是需要改变的路线。 expect_any_instance_of(Hospital).to receive(:update).with({ “name” => “MyString” }) 可能是这样的 expect_any_instance_of(Hospital).to receive(:update).with(params: { “name” => “MyString” }, permitted: true) 我只是不知道语法是什么,并且无法在Rails 5的分散文档中找到它,或者在Rails 5上有关RSpec的不存在的注释/ Stack Overflow问题。 完整错误和控制器规格 2) HospitalsController PUT update with valid params updates the requested hospital Failure/Error: if @hospital.update(hospital_params) # received :update with unexpected arguments expected: ({“name”=>”MyString”}) […]

如何在简单表单中的每个单选按钮之前添加标记?

简单的必要性,但显然没有简单的方法来使用Simple Form。 我需要在使用Simple表单的Rails表单中的每个无线电input之前放置一个span 。 我已按照本指南的说明成功地为文本输入添加了一个span ,但我无法弄清楚如何使用单选按钮(以及复选框)执行相同的操作。 我不想使用jQuery,我目前正在使用它作为一种解决方法。 更新1 正如@TarynEast在评论中提到的那样,我尝试使用以下代码创建初始化程序,我已经能够将span标记添加到字符串input 。 初始化 # config/initializers/simple_form__spanner class SpannerInput < SimpleForm::Inputs::Base def input(wrapper_options) merged_input_options = merge_wrapper_options(input_html_options, wrapper_options) spanner_input = "” spanner_input << @builder.text_field(attribute_name,merged_input_options) return spanner_input.html_safe end end 视图 # the view code HTML输出 # the output in view XXX 现在我希望能够用单选按钮和复选框做同样的事情。 输出应该是这样的: 更新2 (几乎是解决方案) 我有一些东西可以以某种方式做我想要的,即在一组单选按钮或复选框中每个输入有一个span 。 它在input标签之后输出跨度,但在文本之前输出它有点起作用。 该解决方案不会使用任何初始化程序,它只是为集合中的每个对象运行的lambda 。 假设你有一个调用choices的数组: […]

AWS Rails安装失败:activesupport需要Ruby版本> = 2.2.2

我在aws上安装rails 5。 我已经安装了rvm和ruby。 我安装的Ruby版本是2.3.1。 我已经完成了捆绑安装。 现在我尝试使用命令安装rails 5 – ‘sudo gem install rails –version 5.0.0’ 但我收到了错误 ERROR: Error installing rails: activesupport requires Ruby version >= 2.2.2. rvm列表 =* ruby-2.3.1 [ x86_64 ] 完成登录运行命令 – ‘sudo gem install rails –version 5.0.0’ Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully […]

如何在不使用Asset Pipeline的情况下在Rails 5中提供静态图像?

我有一个基于Web的应用程序,后端构建在Rails 5上。我使用AngularJS作为前端。 我没有使用Asset Pipeline来提供任何静态内容,我的所有脚本(JS和CSS)都加载到位于public directory的index.html文件中,从那里我使用angular的ng-route来进一步管理位置。 问题出现在我的一个HTML页面中,我需要使用HTML图像标记包含图像 ,问题是服务器找不到图像文件。 我尝试将我的图像保存在app/assets/images和public/assets/images 目录中,但在所有实例中,我得到路由错误,指出我的rails服务器控制台中找不到路由。 参考几个堆栈溢出答案我在我的config development.rb文件中尝试了这一行: config.public_file_server.enabled = true 但它没有用。 那么如何在不使用Rails Assetpipeline的情况下提供图像呢?

RoR – 将param传递给Bootstrap模态。 背景不淡出

我显示配置文件列表,需要通过模式显示每个用户的更多详细信息: :profile_modal, :profile_id_param => profile.id }, {remote: true, ‘data-toggle’ => ‘modal’, ‘data-target’ => ‘#modal-window’} %> 这是模态的容器fiv: 控制器动作: def profile_modal @profile = Profile.find_by_id(params[:profile_id_param]) respond_to do |format| format.js # format.html end end 和profile_modal.js.erb: $(“#modal-window”).html(“”); $(“#profile_modal”).modal(); 莫代尔: 上面的代码根据params[:profile_id_param]值传递变量,但有两个问题: 打开模态并关闭它后,背景不会再次淡入。 它仍然更暗,只是模态本身消失; 出于某种原因,我无法将当地人传递给模态。 如您所见,我在其中使用了实例变量,因为它使用undefined错误。 这有什么不对? 更新: 在点击时关闭这样的turbolinks: :profile_modal, :profile_id_param => profile.id }, {remote: true, ‘data-turbolinks’ => false, ‘data-toggle’ => ‘modal’, […]

Rails api-only忽略rabl文件

我有一个非常简单的应用程序,使用Rails 5(仅限API),但由于某种原因它返回一个空响应,而它应该返回一个json对象(使用RABL) class ExercisesController < ApplicationController before_action :set_exercise, only: [:show, :update, :destroy] def index @exercises = Exercise.all # Works # render json: @exercises # Blank response render :index end end # app/views/exercises/index.json.rabl collection @exercises attributes :id, :name 如果我使用直接渲染(注释)它可以正常工作,但使用rabl模板它是空白的…任何想法?