Tag: ruby on rails 4

强制所有收到的请求Content-Type为JSON

我实际上正在使用一个使用Rails 4的API。如果客户端没有在Content-Type标头中指定媒体类型,我想将请求的Content-Type设置为JSON。 为了获得该行为,我尝试在ApplicationController添加以下before_action : def set_request_default_content_type request.format = :json end 在我的RegistrationsController#create方法中,我有一个断点来检查一切是否正常。 好吧, request.format技巧不起作用,尽管值设置为application/json ,似乎控制器(或Rails内部)不会将接收到的请求的Content-Type视为JSON。 我使用以下正文进行了POST请求(并且没有Content-Type): {“user” : {“email”:”foobar@mail.net”,”password”:”foobarfoo”}} 通过Pry调试我看到: [2] api(#) _ request.format.to_s => “application/json” [3] api(#) _ params => { “action” => “create”, “controller” => “v1/registrations” } 这意味着Rails没有考虑我的请求,使用Mime::JSON配置了request.format,而是使用Mime::ALL ,因此它没有解析请求的JSON主体。 🙁

Rails 4:如何使用includes()和where()来检索关联对象

我无法弄清楚如何使用.where()方法来检索关联的模型数据。 在此示例中,Projects belongs_to Users … class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base has_many :projects end class ProjectsController params[:id]} ).first end end 在App / views / projects / invite.html.erg 返回: — !ruby/object:Project attributes: id: 22 name: Some Project Name belongs_to: 1 instructions: Bla bla bla active: true max_duration: 2 max_videos: […]

Rails has_many通过表单和其他属性

我正在尝试创建一个表单,允许用户向广告系列添加/编辑/删除位置。 我目前找到的所有示例都是针对HABTM表单(不允许编辑has_many through配置中存在的其他属性)或仅列出现有关系。 下面的图片显示了我想要完成的任务。 该列表将显示每个可用位置。 将检查通过campaign_locations模型建立关系的位置,并使其campaign_location特定属性可编辑。 应该能够检查未经检查的位置,输入的campaign_location特定数据以及在提交时创建的新关系。 以下是我目前实施的代码。 我尝试过使用collection_check_boxes ,这非常接近我的需要,除了它不允许我编辑campaign_location属性。 我已经能够成功编辑/删除现有的campaign_locations,但我无法弄清楚如何将其合并以显示所有可用的位置(如附图)。 楷模 campaign.rb class Campaign < ActiveRecord::Base has_many :campaign_locations has_many :campaign_products has_many :products, through: :campaign_products has_many :locations, through: :campaign_locations accepts_nested_attributes_for :campaign_locations, allow_destroy: true end campaign_location.rb class CampaignLocation < ActiveRecord::Base belongs_to :campaign belongs_to :location end location.rb class Location < ActiveRecord::Base has_many :campaign_locations has_many :campaigns, through: :campaign_locations […]

Rails“ActiveRecord_Associations_CollectionProxy的未定义方法”

我有这个型号: class Student < ActiveRecord::Base has_many :tickets has_many :movies, through: :tickets end class Movie < ActiveRecord::Base has_many :tickets, dependent: :destroy has_many :students, through: :tickets belongs_to :cinema end class Ticket < ActiveRecord::Base belongs_to :movie, counter_cache: true belongs_to :student end class Cinema < ActiveRecord::Base has_many :movies, dependent: :destroy has_many :students, through: :movies has_many :schools, dependent: :destroy has_many […]

基于用户表单的动态Active Record Store访问器?

Active Record Store允许您序列化单个单元格内的参数。 即 class User < ActiveRecord::Base store :options, accessors: [ :option1, :option2, :another_random_option ] end 现在,所有访问器都在users表的“options”列中序列化。 u = User.new u.option2 = ‘some option’ u.option2 # => ‘some option’ 这对我的应用程序很有用,因为我必须每天创建许多表单,其中90%的表单是相同的(用户名,爱好,兴趣等),然后10%是无模式(random_option_here,another_random_option_in_another_form) 。 我也从不需要按无架构选项排序。 我做的是我为90%的表单字段创建了1个表,它们始终是相同的,然后我有另一个表,其中包含最后10%的字段(我有另一个表的原因是因为这是belongs_to关系,因此用户可以在此表中有许多行)。 … 现在唯一的问题是每次我在自定义表单中创建一个新字段时,我必须将该参数添加到Active Record Store访问器,否则我会得到一个方法缺失错误。 如果我可以进入并为无模式字段创建尽可能多的View表单并且永远不更新模型中的访问器,那将是很好的。 所以我的问题是:是否有动态添加所有用户提交的自定义字段到访问者数组,这样如果用户提交字段“some_random_option1221”,“another_option_here”然后我不必进入访问者数组并添加那个领域? 谢谢!

Rails – 如何仅在某些页面上包含Javascript文件

我有一个WYSIWYG编辑器,我已经内置到网站和自定义。 有许多Javascript文件只需要使用WYSIWYG编辑器加载到页面上; 目前它们被加载到每个页面上(甚至在某些页面上打破其他Javascript)。 目前,Javascript文件位于assets/javascript/wysiwyg/ ,并且不包含在application.js的require文件中,但由于资产管道(我认为),它仍然包含在每个页面中。 我想知道我是否可以从其他页面中排除这些文件。 是否可以将它们从资产管道移动到public/目录并将它们(在咖啡脚本文件中,可能?)导入适当的视图?

ActionView :: Template ::错误:缺少要链接的主机

正如标题所暗示的,当我尝试在我的邮件模板中使用link_to时,我收到此错误: ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true 我试图按照它的说明,我也发现了这篇文章 ,但我仍然收到错误。 我尝试将config.action_mailer.default_url_options = { host: ‘example.com’ }到以下每个文件中,但没有一个工作: /config/environment.rb /config/application.rb (inside the module for my app) /config/development.rb (I’m in development mode) 在Rails 4中有什么变化使得这个工作有所不同吗?

与Faye和Rails的私人消息

我正在使用在railscast中播放的faye gem ,允许应用程序推送消息。 问题是它将消息推送到所有打开的聊天客户端。 我需要他们是私人的。 可以通过faye获取私人消息,但它是基于url的。 例如,所有消息都将发送到site.com/foo 。 但是,在我的模型中,聊天没有特定的url。 因为聊天只是该用户发送给您的邮件集合。 因此,如果您以adam site.com/messages/eve身份登录,那么您可以与前夕交谈,但前夕则相反。 site.com/messages/adam 。 因此,特定于URL的私人聊天似乎是不可能的。 有什么指针吗? messages_controller.rb (触发ajax) def create @message = Message.new(message_params) @message.save end create.js.erb (调用广播方法) $(“.text-wrap”).append(” “); 广播方法 (发布到王菲服务器) def broadcast(user, &block) channel = “/messages/#{user}” message = {:channel => channel, :data => capture(&block)} uri = URI.parse(“http://localhost:9292/faye”) Net::HTTP.post_form(uri, :message => message.to_json) end application.js (faye服务器订阅/ […]

是否仍然建议使用Minitest测试Rails 4中的路由?

在Rails 3中,当在MiniTest中编写function测试时,我习惯于测试路由而不是测试我的控制器操作。 我从Rails测试指南中得到了这个想法- 第9节:测试路由 。 但是,在将我的应用程序升级到Rails 4之后,我注意到如果我没有使用一组适当的参数提供get|patch|post|delete方法,那么控制器操作测试本身已经开始涉及未知路由。 例如,给定路线: # config/routes.rb namespace “api” do namespace “v2”, defaults: { format: :json } do resources :users do resources :posts do resources :comments end end end end 和function测试: # test/controllers/api/v2/comments_controller_test.rb describe Api::V2::CommentsController it “does something” do get :index end end 在Rails 3中,上述方法可行。 但在Rails 4中,我收到了URL生成错误: ActionController :: UrlGenerationError:没有路由匹配{:action =>“index”,:controller =>“api […]

如何让ruby-debug-ide工作?

我无法让我的ruby-debug-ide正常工作。 我有一个基本的rails应用程序,我想在RubyMine中调试。 在我开始使用RubyMine之前,我需要在我的vagrant VM中启用rdebug-ide。 当我导航到我的项目目录时,我已在线阅读我需要输入以下命令: rdebug-ide –port 1236 –dispatcher-port 26166 –host 0.0.0.0 – bin/rails s -b 0.0.0.0 但是,当我运行此命令时,我收到此消息: 我也试过上面命令的修改版本:(额外的破折号) rdebug-ide –port 1236 –dispatcher-port 26166 –host 0.0.0.0 — bin/rails s -b 0.0.0.0 我得到了这个结果: 这看起来很好,但随后服务器永远不会启动。 它从不显示服务器信息,它只是坐在这里并挂起,直到我CTRL + C退出服务器。 我无法让rdebug-ide与rails配合使用。 有谁知道我怎么解决这个问题?