Tag: ruby on rails

当嵌套obj上的show / edit为null时,使用fields_for和accepts_nested_attributes_for

编辑:所以我可以输入表单并单击提交,并在show.html.erb我收到此错误: undefined method ‘address_line_one’ for nil:NilClass 在这一行这里是否意味着没有mailing_address? 即使我在下面的fields_for获得了这些信息,并且应该将其保存在提交中…但我认为我强大的参数在下面确定是阻止了mailing_address被保存。 如果您查看我的模型,您可以看到mailing_address的使用方式(也是我控制器中的新操作) 原帖: 使用此博客来执行嵌套表单: The Definitive Guide to accepts_nested_attributes_for a model in rails 3 它运作得很好,或者似乎也是如此。 新动作渲染得很好,我可以在单击提交时输入信息,表示已保存,并向我显示节目动作 但是节目中的@ group.mailing_address似乎不存在(这意味着点击这里的编辑也不会在field_for中显示任何内容) 我猜这与我的def group_params虽然不确定我做错了什么。 group show rb snippet: Group tin: Group Address: , 团体模特: class Group < ActiveRecord::Base belongs_to :mailing_address, class_name: 'Address', foreign_key: 'mailing_address_id' accepts_nested_attributes_for :mailing_address end 集团控制人: def show #@group = Group.find(params[:id]) […]

Rails和MySQL – ActiveRecord :: ConnectionNotEstablished在生产环境中

我会尽量做空: 将Rails 3.2.1和mysql2用于数据库; 在Ubuntu 11.10上运行 在开发环境中,一切正常 当我在生产中启动我的服务器(WEBrick)时,它启动正常,但在加载任何页面后,它会引发以下错误: ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished): activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in retrieve_connection’ activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection’ activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection’ … /home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service’ /home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run’ /home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread’ 在试图找到任何可能的错误来源,并咨询十亿个不同的网站和类似的报告后,我仍然无法找到错误。 我的database.yml (现在在localhost上运行,并且我的设置在生产环境中与开发相同,哪个有效)很好,我的Gemfile没问题 (包括gem’mysql2 ‘,不用担心),生产数据库是创建和rake db:migrate没有问题迁移它…我没有想到或没有在互联网上找到任何其他可能的错误来源? 我强调,这只发生在生产中 谢谢你的帮助 编辑发布我的Gemfile: source ‘https://rubygems.org’ gem ‘rails’, ‘3.2.1’ gem ‘mysql2’ # Gems used only for assets and not required […]

如何引用我的友谊表以有条件地显示代码?

在跟随Ryan Bates的railscast之后,我已经成功地在我的Ruby on Rails应用程序中为用户建立了友谊自我参与协会。 我可以成功地关注,取消关注,并查看谁在关注。 现在我想引入另一个元素,我不知道该怎么做。 在我的current_user仪表板页面上,我想根据current_user是否跟随任何人以及是否有任何人跟随current_user来有条件地显示元素。 换句话说,如果current_user没有跟随任何人,我不想显示以下内容: Following :delete, :class => “unfollow” %> 如果没有任何人关注current_user,我不想显示: Followers 这些是如何建立关联: Friendship.rb belongs_to :user belongs_to :friend, :class_name => “User” User.rb has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => “Friendship”, :foreign_key => “friend_id” has_many :inverse_friends, :through => :inverse_friendships, :source => :user def friends_workouts @friends_workouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order […]

如何在没有名称列的情况下显示名称而不是ID

我有3个表,Activity,Activity_type和Itineraries。 在活动类型中,我插入一个新的条目名称=> Testing => Example。 在我的活动中,我可以使用我在下拉列表中创建的活动类型并键入位置。 在创建新行程时,会有一个下拉活动和用户(创建新行程并选择特定行程的活动),但它只显示活动#1和用户#1。 在我的列表中,我认为这就是为什么它只显示活动#1而不是测试。 题: 1)在创建新行程时,如何将活动#1更改为特定名称?(例如:在下拉列表中,它应该是测试而不是活动#1) 2)如何在没有名称列的情况下显示名称而不是ID? 活动表 activity_type_id location_id 行程表 title created_by 活动类型表 name description 注意:我也在下面尝试过,但没有任何反应 Rails管理员 config.model Itinerary do list do field :title # field :schedule field :no_of_days field :activity field :user # field :location # field :activity_type # field :location_name end create do field :title end object_label_method do […]

选择要在模型上具有唯一ID的值

我有两种型号特卖和餐厅 我想显示每家餐厅的优惠。 我不希望它们链接:restaurant_id ,而是我想要它们链接:restaurant_name 。 我无法弄清楚如何链接它们:restaurant_name控制器中的:restaurant_name restaurant controller def show @restaurant = Restaurant.find(params[:id]) @deals = @restaurant.deals end show.html.erb 有任何想法吗?

ActiveRecord属性消失的情况

按照https://stackoverflow.com/a/24496452/102675中的说明,我结束了以下内容: namespace :db do desc ‘Drop, create, migrate, seed and populate sample data’ task seed_sample_data: [:drop, :create, :migrate, :seed, :populate_sample_data] do puts ‘Sample Data Populated. Ready to go!’ end desc ‘Populate the database with sample data’ task populate_sample_data: :environment do puts Inspector.column_names.include?(‘how_to_fix’) # create my sample data end end 正如您所料,如果我运行bundle exec rake db:populate_sample_data ,我就会成功 但是如果我运行bundle […]

Rails form_with导致NoMethodError

我正在重新构建一个博客,并且遇到了有关创建新文章的forms的问题。 导致错误undefined method `form_with’ 。 Rails提供了以下建议: Did you mean? form_tag Did you mean? form_tag 在尝试form_tag ,我收到以下错误: undefined method `label’ for nil:NilClass 使用form_with的新文章的表单 New Blog Post articles_controller.rb class ArticlesController < ApplicationController def index @articles = Article.all end def show @article = Article.find(params[:id]) end def new @article = Article.new end def create @article = Article.new(article_params) @article.save redirect_to […]

如何捕获上传的文件名并在RoR应用程序中显示?

我正在尝试创建一个允许我上传excel文件的ROR应用程序。 我希望能够列出所有上传的excel文件的名称,并带有可点击链接到每个名称,这些名称将带我到show.html.erb页面,该页面将显示该文件的数据。 我无法捕获上传的文件(报告)的名称,并将其等同于title_name,以便可以在index.html.erb页面中列出。 这是我的架构: ActiveRecord::Schema.define(version: 20170402043056) do create_table “files”, force: :cascade do |t| t.string “Name” t.datetime “created_at” t.datetime “updated_at” end create_table “reports”, force: :cascade do |t| t.string “date” t.string “order” t.string “item” t.string “product” end end 我已将此添加到routes.rb: resources :reports do collection { post :import } end 报告模型: class Report< ActiveRecord::Base require 'roo' belongs_to :file def […]

为什么yaml文件上传不起作用? 我认为许可问题

我想加载config目录,用户选择的yaml文件。 我有一个观点: File Upload Upload your Yaml Configuration File { :multipart => true } do |f| %> Select File: ‘Uploading…’ %> 我有一个控制器: class UploadController < ApplicationController def uploadFile uploaded_io = params[:uploadFile][:upload] File.open(Rails.root.join('public', uploaded_io.original_filename), 'w') do |file| file.write(uploaded_io.read) end flash.now[:notice]="File has been uploaded successfully" end end 当用户选择文件并按“上传”时,没有任何反应,文件不在配置文件夹中。 我能做什么? 也许我应该使用“import yaml”并以某种方式加载它,怎么样? 日志说: Started POST “/upload/uploadfile” for 127.0.0.1 […]

为什么我的Rspec发布:创建无效的参数控制器测试失败?

我的控制器有两个测试。 spec/controllers/employees_controller_spec.rb it ‘should create a new valid employee’ do employee_params = FactoryGirl.attributes_for(:employee) expect { post :create, employee: employee_params }.to change(Employee, :count).by(1) should redirect_to(action: :index) end it ‘should not save an employee with an invalid avatar extention’ do invalid_employee_params = FactoryGirl.attributes_for(:employee) invalid_employee_params[:avatar] = Rack::Test::UploadedFile.new(File.open(File.join(Rails.root, ‘/spec/fixtures/photos/duck.txt’))) expect { post :create, employee: invalid_employee_params }.to_not change(Employee, :count).by(1) end […]