【问题标题】:My rails form is not being submitted or redirected我的 Rails 表单没有被提交或重定向
【发布时间】:2019-01-24 08:43:11
【问题描述】:

我的邀请表单没有被保存。没有错误。邮件没有被发送,我也没有被重定向到根路径。

邀请控制器:

class InvitesController < ApplicationController
  def new
    @invite = Invite.new
  end

  def create
    @invite = Invite.new(invite_params)
    if @invite.save
      InviteMailer.invite_user(@invite).deliver_now
      flash[:success] = "You have successfully sent an invite"
      redirect_to root_path
    else
      render 'new'
    end
  end

  private

    def invite_params
      params.require(:invite).permit(:email)
    end
end

邀请模特:

class Invite < ApplicationRecord
  belongs_to :user
end

邀请新视图:

<h1>Invite your friend!</h1>
<%= form_for @invite , :url => userinvite_path do |f| %>
    <%= f.label :email %>
    <%= f.email_field :email %>
    <%= f.submit "Send" %>
<% end %>

邀请邮件人:

class InviteMailer < ApplicationMailer
  def invite_user(invite)
    @invite = invite
    mail to: invite.email, subject: "Invitation to Math-Scientist"
  end
end

应用程序邮件:

class ApplicationMailer < ActionMailer::Base
  default from: 'noreply@example.com'
  layout 'mailer'
end

邮件视图(文本): 你好, 您的朋友已邀请您加入 Math-Scientist。 立即注册:https://math-scientist.herokuapp.com/usersignup 希望您喜欢我们的产品!

路线文件:

Rails.application.routes.draw do
  root 'static_pages#home'
  get '/usersignup', to: 'users#new'
  get '/companysignup', to: 'companies#new'
  get    '/userlogin',   to: 'sessions#new'
  post   '/userlogin',   to: 'sessions#create'
  delete '/userlogout',  to: 'sessions#destroy'
  get '/userinvite', to: 'invites#new'
  post '/userinvite', to: "invites#create"
  resources :users
  resources :companies
  resources :invites
end

【问题讨论】:

  • 你的模型一定有问题。我怀疑由于模型关系而回滚。您是否检查过服务器日志以查看是否实际发生了任何回滚?因为如果是这样,那么if @invite.save 会得到false,你应该被重定向回new
  • 我删除了邀请和用户之间的模型关联,因为我并不真正需要它。这次提交了表单,我被重定向了,但邮件仍然没有发送。
  • 你知道pry怎么用吗?将gem 'pry' 添加到您的 Gemfile。并在控制器中的else 之后添加binding.pry。然后运行你的测试并观察控制台。然后你可以检查@invite.errors
  • 这是我的 production.rb 文件:config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp host = 'math-scientist.herokuapp.com' config.action_mailer.default_url_options = { host : 主机 } ActionMailer::Base.smtp_settings = { :address => 'smtp.sendgrid.net', :port => '587', :authentication => :plain, :user_name => ENV['SENDGRID_USERNAME'], :密码 => ENV['SENDGRID_PASSWORD'], :domain => 'heroku.com', :enable_starttls_auto => true }
  • 您正在将您的 sendgrid 凭据 加载为 ENV 变量。您是否已将它们添加到 Heroku?因为如果你没有 rails 不知道它们并且无法与 sendgrid smpt 通信

标签: ruby-on-rails ruby


【解决方案1】:

我猜邀请没有被保存,因为你有一个belongs_to :user 关系。由于 Rails 5,默认情况下这是必需的。这意味着或者你必须在保存之前设置user_id,或者指定它是可选的。

不要将其设置为表单的用户 ID(在隐藏字段中),因为这会导致篡改(例如,有人在提交表单之前对其进行了编辑)。

所以在你的控制器中你可以做类似的事情

@invite = Invite.new(invite_params)
@invite.user_id = current_user.id 
if @invite.save ...

或者,如果user 不是真正需要的,您也可以调整您的模型

belongs_to :user, optional: true 

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多