Tag: ruby on rails 4

Rails 4如何从Model调用accessible_attributes

嗨,我在Rails演员( 导入csv Excel文件 )上尝试这个tuto。 我在这行product.attributes = row.to_hash.slice(*accessible_attributes)上有错误 对于#`, undefined local variable or method accessible_attributes’ 这是我的模特。 class Product < ActiveRecord::Base require 'roo' validates_presence_of :price #attr_accessible :name, :price, :released_on #I removed this line (rails 4) def self.to_csv(options = {}) CSV.generate(options) do |csv| csv << column_names all.each do |product| csv << product.attributes.values_at(*column_names) end end end def self.import(file) spreadsheet […]

param丢失或值为空

我是Rails的新手。 得到以下错误。 我明白这个问题是什么,但不确定如何修复它? 错误 – 参数丢失或值为空:客户 def customer_params params.require(:customer).permit( :first_name, :last_name, :email, :password, :password_confirmation) end end DetailsController.rb class My::Account::DetailsController :show else redirect_to my_account_details_path, :notice => ‘Account details updated’ end end private def customer_params params.require(:customer).permit( :first_name, :last_name, :email, :password, :password_confirmation) end end 视图 .account.container .row = render :partial => ‘/my/account/sidebar’ .section %h2 Your account details = […]

如何在Ruby on Rails中为模型添加虚拟属性?

我正在研究RubyonRails / ActiveAdmin应用程序。 我的RoR版本是4.2.5,AA版本是1.0.0。 我有一个模型Message如下。 class Message < ActiveRecord::Base belongs_to :user validates :user, :content, presence: true def palindrome # return true/false end end 如您所见,我想要一个只读属性palindrome ,它只取决于消息的content 。 我希望将此属性视为与普通属性完全相同。 通常,我的意思是当我通过rails console检索消息或请求消息的json格式时,我想在列表中看到一个palindrome属性。 我还希望通过此属性对消息进行过滤。 我不知道怎么能实现这个目标。

对rails中的特定控制器跳过/禁用force_ssl

我需要在我的应用程序中的所有路由上强制使用SSL,除了message#new controller。 在config/environments/production.rb ,我有: config.force_ssl = true 现在所有路由都重定向到https,现在我想禁用message#new controller。 有谁知道如何在Rails 4+应用程序中禁用特定控制器的强制SSL?

要使博客在rails 4.2中运行,我需要做些什么?

我刚安装了导轨4.2。 我找到了这个教程来制作一个快速的博客: https : //www.reinteractive.net/posts/32-ruby-on-rails-3-2-blog-in-15-minutes-step-by-step 。 但是,它使用rails 3.2。 我已经完成了它所说的所有rake db:migrate ,然而,当我运行服务器时,我只是得到一个错误页面。 自3.2以来有什么变化? 我现在要做什么来做同样的事情? 错误:post中的’ExecJS :: ProgramError #index’TypeError:对象不支持此属性或方法(在C:/Ruby193/lib/ruby/gems/1.9.1/gems/turbolinks-2.5.3/lib/资产/ Java脚本/ turbolinks.js.coffee) 编辑: 在旁注,我甚至不能遵循官方ruby on rails教程,因为当我运行服务器时,在将root更改为root’welcome root ‘welcome#index’ ,我只是得到一个页面未找到错误。 有关rails 4.2的教程吗?

根据点击的按钮渲染三个不同的部分

所以我有这样的布局: 家用/ index.html.erb: All posts All stories 在posts_controller.rb中 我有: def index @posts = Post.all respond_to do |format| format.html #looks for views/books/index.html.erb format.js #looks for views/books/index.js.erb end end 在stories_controller.rb中 我有: def index @stories = Story.all respond_to do |format| format.html #looks for views/books/index.html.erb format.js #looks for views/books/index.js.erb end end 在我的views / posts / index.js.erb中 $(“#content”).html(“”); 在我的views / […]

在Rails中添加可以为空的外键

引用Rails 4.2 add_foreign_key支持: # add a foreign key to `articles.author_id` referencing `authors.id` add_foreign_key :articles, :authors 如何创建一个可以为空的外键约束,以允许这样的情况,其中articles.author_id有时可以为null?

仅允许管理员用户使用Devise在Rails中创建新用户(无外部模块)

目前,我的Users数据库有一个名为“admin”的列,其中包含一个布尔值,默认设置为false。 我有一个管理员用户播种到数据库中。 如何编写我的应用程序,以便管理员用户可以创建新用户,但不是的用户不能? (另外,用户只能由管理员创建) 似乎应该有一个简单的方法来设计,不涉及使用一些外部模块。 但到目前为止,我还没有找到满意的答案。 我更有可能标记出只有设计的解决方案。 (其中一个只是标准的MVC / Rails解决方案加一个)然而,如果真的有更好的方法来做不涉及CanCan我也可以接受。 注意: 我一直在寻找一段时间,我发现了其他几个与此问题非常类似的stackoverflow问题,但要么没有完全回答问题,要么使用其他非设计模块。 (或两者)

将UTF-8设置为Ruby 1.9.3的默认值

我在Rails 4和Ruby 1.9.3上 我经常使用“奇怪”字符,所以我必须在所有.rb文件的顶部声明UTF-8编码。 有没有办法将UTF-8设置为Ruby 1.9.3的默认编码? 我尝试了所有答案,但是当运行rake db:seed并创建一个属性包含非US-ASCII有效字符的对象时,我仍然收到此错误: `block in trace_on’: invalid byte sequence in US-ASCII (ArgumentError)

ActiveRecord :: AdapterNotSpecified数据库配置未指定适配器

当我使用heroku打开我的web应用程序工作正常,但当我使用rails(localhost)时,我遇到了这个错误: ActiveRecord::AdapterNotSpecified database configuration does not specify adapter 为什么是这样? 这是我的database.yml # PostgreSQL. Versions 8.2 and up are supported. # # Install the pg driver: # gem install pg # On OS X with Homebrew: # gem install pg — –with-pg-config=/usr/local/bin/pg_config # On OS X with MacPorts: # gem install pg — –with-pg-config=/opt/local/lib/postgresql84/bin/pg_config # On Windows: […]