Tag: ruby on rails 4

添加字段以使用Rails设计注册4

首先,我是铁杆的新手,所以我仍然掌握着一切。 我正在尝试将两个字段添加到Devise注册页面,第一个名称字段和一个姓氏字段。 我继续前进并更改了视图和控制器以尝试此操作。 它们看起来像这样: 控制器: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_action :configure_devise_permitted_parameters, if: :devise_controller? protected def configure_devise_permitted_parameters registration_params = [:firstName, :lastName, :email, :password, :password_confirmation] if params[:action] == 'update' devise_parameter_sanitizer.for(:account_update) { |u| u.permit(registration_params << :current_password) } […]

Rails 4.2外键

Rails 4.2新增支持添加和删除外键(在迁移中),例如: # add a foreign key to `articles.author_id` referencing `authors.id` add_foreign_key :articles, :authors 我不明白的是:这是怎么回事 add_foreign_key :articles, :authors 不同于此: add_column :articles, :author_id, :integer 谢谢你的任何澄清!

Rails 4:如何使用carrierwave上传多个图像

我可以使用carrierwave上传文件,如下面的代码所示。 如何编辑代码,以便在一篇文章中上传最多3个文件的图像? 意见\文章\ new.html.erb . . . . views \ shared \ _article_form.html.erb . . . . \型号\ article.rb class Article < ActiveRecord::Base belongs_to :user belongs_to :category has_many :photos, dependent: :destroy accepts_nested_attributes_for :photos . . end \型号\ photo.rb class Photo < ActiveRecord::Base belongs_to :article mount_uploader :image, ImageUploader validates :image, presence: true end \控制器\ articles_controller.rb class […]

模型中的更改未保存。 Rails 4.1

我刚刚使用脚手架创建了一个用户模型,如下所示: rails g scaffold user name:string 我运行db:migrate并且一切正常,但是当我尝试添加新字段时,它不会将其保存在数据库中。 我通过这样做来添加字段: rails g migration add_surname_to_users surname:string 当我运行rake db:migrate时,没有奇怪的消息,数据库表没问题,表单已被编辑为alow surname输入,因为它有show和index视图来正确显示它。 但是,它不会保存给予数据库的表单值。 关于如何解决它的任何线索? 不能改变我的模型真烦人。

Ruby on Rails 4:在搜索结果页面上显示搜索结果

我目前正在项目中实现搜索function,我正在努力在专门的搜索结果页面上显示它。 已经意识到关于这个主题的问题,但由于完全无能而无法找到解决方案,我问你最后的指针:)。 搜索表单在索引页面上生成,该页面是entries_path和root_path。 我想将参数传递给新页面search_path。 这是我的文件: EntriesController def search end def index @entries = Entry.all.order(‘entries.created_at DESC’) @entry = Entry.new # My index page also creates new entries. if params[:search] @entries = Entry.search(params[:search]).order(“created_at DESC”) else @entries = Entry.all.order(“created_at DESC”) end 型号:entry.rb def self.search(search) where(“content LIKE ? OR created_at LIKE ?”, “%#{search}%”, “%#{search}%”) end 的routes.rb Rails.application.routes.draw do resources […]

Rails新对象是零

我再次玩rails并发现这种行为,当我创建一个具有某些属性的Post模型的新实例时,它告诉我所有属性都是零,为什么会发生? Loading development environment (Rails 4.0.0) 2.0.0-p451 :001 > a = Post.new(title: “Rails”, content: “Rails Post”) => # 2.0.0-p451 :002 > a.title => “Rails” 2.0.0-p451 :004 > a.content => “Rails Post” 2.0.0-p451 :005 > a.inspect => “#” 2.0.0-p451 :006 > a.errors.messages => {} 2.0.0-p451 :007 > a.valid? => true class Post < ActiveRecord::Base attr_accessor :title, […]

rails中的质量分配问题

我正在阅读下面这篇文章。 http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments 下面是我们创建一个新的Comment对象并与相应article associate的代码。 @comment = Comment.new @comment.article_id = @article.id 任何人都可以解释一下作者试图在下面说的话。 任何人都可以告诉我如何在没有安全事情的情况下做更好的理解。 由于Rails的质量分配保护,需要手动为新的Comment对象的article_id属性分配Article的id 。

如何访问新的狂欢模型

我是狂热的新手,我创造了一个新的狂欢模型 module Spree class VerificationDocument < ActiveRecord::Base end end 当我尝试从控制台访问模型时,它显示错误 NameError: uninitialized constant Spree::VerificationDocument 不知道我错过了什么。

路由约束不会正确地为Rails参数赋值; 使用’+’分隔值

我希望能够在我的应用程序路由中支持国家/地区代码和区域代码。 例如: /实体/美 /实体/我们+ CA /实体/ US /分钟 /实体/ US / MN +无线+ IA /实体/我们+ CA / BC + WA 我目前的路线: get “/entities/:country_code/(:region_code)” => “entities#index”, :constraints => {:country_code=>/[a-zA-Z]{2}[\+\,]?/, :region_code=>/[a-zA-Z]{2}[\+\,]?/} resources :entities 尝试/entities/us+ca导致此exception: # Use callbacks to share common setup or constraints between actions. def set_entity @entity = Entity.find(params[:id]) end Application Trace | Framework Trace | […]

使用Prawn创建包含属于collectionproxy的值数组的PDF

Ruby on Rails 4 创建带有问题和答案的PDF。 问题很好。 现在我试图将相关答案放在每个问题的旁边或下面。 我能够在每个问题上显示一个答案,现在我遇到了问题,因为答案存储在数组中。 控制器: def show @all_answers = [] @test = Test.find(params[:id]) @test_questions = @test.questions @test_questions.each do |a| @all_answers << Answer.find_all_by_question_id(a) end respond_to do |format| format.html format.pdf do pdf = TestReport.new(@test_questions, @all_answers) send_data pdf.render, filename: 'certification_test.pdf', type: 'application/pdf' , disposition: 'inline' end end end PDF类代码: class TestReport < LayoutPdf def […]