Tag: ruby on rails 4

将student_id连接到Rails中的submission_id

我试图将我的student_id连接到submission_id,但它不起作用。 我有一个名为submissionstudent的连接表。 学生Controller.rb: class StudentsController < ApplicationController before_action :set_student, only: [:show, :edit, :update, :destroy] # GET /students # GET /students.json def index @students = Student.all end # GET /students/1 # GET /students/1.json def show end # GET /students/new def new @student = Student.new end # GET /students/1/edit def edit end # POST /students # POST […]

#..Rails 4的未定义方法`model_name_question’

我在提交表单后收到此错误:(在索引页面中) 3, :style => “width:80%”, :placeholder => “enter your question.” %> 我有question模型: class Question < ActiveRecord::Base validates :question, presence: true belongs_to :category belongs_to :questioner end 和问题控制器: class QuestionsController < ApplicationController def index @quiz = Question.new @questioner = Questioner.new end def new @quiz = Question.new(quiz_params) end def show @quiz = Question.find(params[:id]) end def edit @quiz = […]

使用find_by方法检索多个记录

以下方法适用于通过电子邮件发送令牌链接的用户并对其进行身份validation。 def login inv = Invitation.find_by(email: params[:email]) if inv && inv.authenticated?(:invitation, params[:id]) @organization = inv.organization unless @organization.nil? render ‘profiles/show’ and return else flash[:danger] = “Error” redirect_to root_path end else flash[:danger] = “Invalid link” redirect_to root_path end end 然而,这种方法似乎假设一个人( inv )只能在Invitation数据库中存在一次。 情况并非如此,同一个人/电子邮件地址可以在数据库中拥有多条记录。 因此,我需要重写方法来解决这种情况。 我怎样才能做到这一点? 我可以在下面第2行添加.each并使用.each吗? def login inv = Invitation.find_by(email: params[:email]).all if inv inv.each do |person| […]

遍历数组以创建rails对象

我无法找到有关如何遍历数组和创建对象的任何信息。 我的表单创建了一个可选择的用户列表,在选中时,将user_ids作为数组对象传递。 邀请\ new.html.rb @event_selected.id %> 我想通过将attend_event_id与attendee_id数组中的所有对象相结合来创建新的Invitations对象。 经过一些麻烦后,我得到了控制器工作的基础知识,但只是将user_id作为文本条目传递。 以下是我的邀请控制器。 不知道从哪里开始,因为我还没有找到一个好的例子。 invitations_controller.rb def create @invitation = Invitation.new(invite_params) if @invitation.save! flash.now[:success] = “Invited!” redirect_to root_path else flash.now[:error] = “Failure!” redirect_to root_path end end private def invite_params params.require(:invitation).permit(:attended_event_id, :attendee_id) end end

SQLite3 :: SQLException:没有这样的表:messages:SELECT“messages”。* FROM“messages”

我正在制作一个基于此railscast的简单聊天应用程序。 我发布了另一个关于此的问题,但我愚蠢地没有添加模型,然后我将其命名为错误。 但是,现在已经修复了。 问题是,我甚至不确定我是否需要数据库。 我本来打算完成这个,然后将它上传到heroku上就可以玩了。 我不想存储消息,但如果有必要,那么我会。 这是我的代码。 index.html.erb Hack Chat 控制器; class MessagesController < ApplicationController def index @messages = Message.all end def create @message = Message.create!(params[:message]) PrivatePub.publish_to("/messages/new", "alert('#{@message.content}');") end end 模型; class Message < ActiveRecord::Base end 航线; Hackchat::Application.routes.draw do root to: ‘messages#index’ resources :messages end 的Gemfile; source ‘https://rubygems.org’ gem ‘rails’, ‘4.0.0’ gem “rake”, “~> 10.1.1” […]

Rails 4错误:ArgumentError – 参数数量错误

我是rails的新手,目前正在编写我的第一个应用程序。 我创建了一个搜索来搜索我的用户(用户名或标签 – 使用acts_as_taggable_on gem)。 出现以下错误: ArgumentError in UsersController#index wrong number of arguments (0 for 1) 用户模型 # Search def self.search(search) if search where([“username LIKE ?”, “%#{search}%”]) elsif searchtags none else all end end # Search Tags def self.searchtags(searchtags) if searchtags tagged_with([“#{searchtags}”], :any => true, :wild => true) else none end end 用户控制器 def index […]

Rails返回所有二级关联

我目前有三种我感兴趣的模型:Region,Seat和User。 地区有很多座位,座位有很多可用的用户。 我想知道在Rails中检索位于特定区域下的座位的所有可用用户的最有效方法是什么。 提前致谢。

为什么我的文本框在表单中显示ActiveRecord_Associations_CollectionProxy

我正在构建rails应用程序,我使用form_for来呈现表单。 一切正常工作正常,除了一个文本字段在文本中有ActiveRecord_Associations_CollectionProxy ,我不知道它来自哪里。 这是我的表格 prohibited this user from being saved: 这是我的Video模型 class Video < ActiveRecord::Base belongs_to :user has_many :video_tags has_many :tags, through: :video_tags end 这是截图

自动完成关闭密码字段

我有一个用于编辑用户详细信息的表单。 当我选择表单时,密码字段已经在其表单字段中显示密码..所以,当我只是更改其他的东西,然后尝试保存密码也进行更新..我怎么能将密码字段留空…到目前为止,我试过了..但没有运气

在特定日期时间延迟作业

我想通过delayed_job发送一些电子邮件 但是,我想在活动前后发送它们。 我担心的是,这实际上是否有效: def one_week_before_run AtendeeMailer.delay(run_at: ‘8th October 2016′.to_datetime).mudrun_about_to_start(self) end def thank_you_note AtendeeMailer.delay(run_at: ’18th October 2016’.to_datetime.end_of_day).thank_you(self) end 或者我应该选择另一种方法?