Tag: ruby on rails 4

Rails 4在保存时创建关联对象

在保存新主对象后,如何自动创建多个关联对象? 例如 在Rails 4中,我有三个对象: 企业 , 预算和类别 。 #app/models/business.rb class Business < ActiveRecord::Base #attrs id, name has_many :budgets end #app/models/budget.rb class Budget < ActiveRecord::Base #attrs id, business_id, department_id, value belongs_to :business belongs_to :category end #app/models/category.rb class Category < ActiveRecord::Base #attrs id, name has_many :budgets end 当我创建新业务时,在保存新业务之后,我想为每个类别自动创建预算并给它$ 0的值。 这样,当我去展示或编辑新的业务时,它将已经具有相关的类别和预算,然后可以对其进行编辑。 因此,在创建新业务时,将创建多个新预算,每个类别对应一个,每个都具有值0。 我读了这篇文章: Rails 3,如何在创建主记录后添加关联记录(Books,Auto Add BookCharacter) 我想知道我是否应该在Business模型中使用after_create回调,然后在Budgets控制器中存在逻辑(不完全确定如何执行此操作),或者我是否应该在’new’中添加logic_controller.rb的逻辑用类似的东西打电话: […]

paperclip找不到“file.jpg”rails 4的处理程序

我在其中一个应用程序中设置paperclip gem时遇到问题。 当我尝试保存上传的文件时,我No handler found for错误消息的No handler found for 。 我做过的事情 移民: add_attachment :orders, :file 模型: has_attached_file :file validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/ 控制器: def order_params params.require(:order).permit(:file) 视图: form_tag url, method: :post, html: {multipart: true} … = file_field_tag ‘order[file]’, disabled: true, id: ‘mtd_file’,accept: ‘image/png,image/gif,image/jpeg’ 当我尝试在我的控制器中执行Order.create params[order]我收到错误 找不到“file.jpg”的处理程序 当我查看order[file] param时,我发现它是一个字符串file.jpg (上传文件的名称)。

Rails控制器inheritance与关注和混合

我的Rails应用程序中有很多类似的资源,我目前使用控制器inheritance来干掉代码。 我看到控制器文件夹下有一个名为Concer的目录,在那里我可能写出类似的问题(比如归档,激活/去激活等)。我也可以编写mixins。 是否有一种首选方法来干扰控制器代码? 使用inheritance有什么缺点,或使用其他技术有什么优势?

Ruby 2.3 – 向net:http请求添加超时错误和通知

我有一个工作系统来产生错误并将它们发送给Active Admin使用。 例如,在Active admin中,对于CMS的特定页面,页面可能会执行: url_must_be_accessible(“http://www.exmaple.com”, field_url_partner, “URL for the partner”) 这使用下面的代码向Active Admin Editor发送不同类型的错误通知: module UrlHttpResponseHelper def url_must_be_accessible(url, target_field, field_name) if url url_response_code = get_url_http_response(url).code.to_i case url_response_code when -1 # DNS issue; website does not exist; errors.add(target_field, “#{field_name}: DNS Problem -> #{url} website does not exist”) when 200 return when 304 return else errors.add(target_field, “#{field_name}: #{url} […]

Rails 5:Jquery自动完成

我在rails应用程序中实现了jquery自动完成function。 这是设置: //= require jquery //= require rails-ujs //= require jquery-ui //= require bootstrap //= require_tree . items.js $(document).ready(function() { $(“#search”).autocomplete({ source: “/search_suggestions”, autoFocus: true, select: function(event, ui) { $(event.target).val(ui.item.value); $(‘#search’).closest(“form”).submit(); return false; } }); }); 这是模型items.rb中的搜索操作 def self.search(term) return where(“0=1”) if term !~ /\w{4}/ where(“lower(title) LIKE lower(:term)”, term: “%#{term}%”) end 之后我创建了一个search_suggestions资源: rails g resource […]

无法在视图Rails 4中显示我的类别和子类别

我的网站有分类和子类别,这里是我在我的种子.rb,我创建了一个名为“video和动画”的主要类别,和4个子类别,比我将子类别分配到主类别。 @category = Category.create!(name: “Video and animation”) [“Intro”, “Animation & 3D”, “Editing and Post Production”, “Other”].each do |name| @subcategory = Subcategory.create!(name: name, category_id: @category.id) end 它运行良好,在我的rails控制台中,我看到一切正常,添加产品的category_id更改为数字(整数应该如此)。 问题:如何在我的视图中显示该类别,因此当有人点击它时,他会获得该类别的所有产品。 我如何按照相同的原则显示所有子类别。 以下是我试图提出意见的内容 奇怪的是,当我把我的产品控制器放进去时 def index @category_id = Category.find_by(name: params[:category]) @gigs = Gig.where(category_id: @category_id).order(“created_at DESC”) @subcategory_id = Subcategory.find_by(name: params[:subcategory]) @gigs = Gig.where(subcategory_id: @subcategory_id).order(“created_at DESC”) end 它显示了我想要的所需子类别,但主要类别仍为空。 如果我把它放入我的控制器 def index […]

Rails模型中的class_name foreign_key

我最近遇到过这段代码。 用户有很多答案。 :class_name和:foreign_key的目的是什么? class Answer ‘Question”, :foreign_key => ‘question_id’ end

Hartl Ruby on Rails教程4 ch 10,用户编辑测试失败

我正在尝试使用Hartl的教程创建一个自定义网站,从而练习Ruby on Rails。 到目前为止,情况一直很顺利,但我在用户编辑测试“使用友好转发成功编辑”中收到错误,其中用户在登录后未被重定向回编辑页面。这是我收到的错误消息: FAIL[“test_successful_edit_with_friendly_forwarding”, UsersEditTest, 3.1409137547016144] test_successful_edit_with_friendly_forwarding#UsersEditTest (3.14s) Expected response to be a redirect to but was a redirect to . Expected “http://www.example.com/users/787258272/edit” to be === “http://www.example.com/users/787258272”. test/integration/users_edit_test.rb:23:in `block in ‘ 我觉得这个解决方案非常简单,我错过了,但我似乎无法弄清楚它是什么。 这是我的users_controller: class UsersController < ApplicationController before_action :logged_in_user, only: [:edit, :update] before_action :correct_user, only: [:edit, :update] def show @user = User.find(params[:id]) end def […]

Rails 4 Devise重新激活“软删除”帐户

按照本教程,如何进行软删除 ,这是有效的。 但是,如果用户决定要返回并重新激活帐户,那么最好的方法是什么? 或者这也有维基​​? 我找不到它。

需要帮助来理解`has_and_belongs_to_many`

数据库中有三个表: 用户(非空) 时间表(非空) schedules_users( 空 ) 用户模型: class User < ActiveRecord::Base […] has_and_belongs_to_many :schedules#has_many :schedules […] end 时间表模型: class Schedule < ActiveRecord::Base has_and_belongs_to_many :users#belongs_to :user end welcome-controller 🙁我希望按日期和时间对日程安排进行排序) class WelcomeController < ApplicationController def index if(current_user) @user_schedules = current_user.schedules @user_schedules_date = @user_schedules.order(:date_time).group_by { |sched| sched.date_time.beginning_of_day } end end end @user_schedules = current_user.schedules这不正确,不是吗? SQL输出: Started GET “/” […]