Rails联系表单

时间:2016-04-14 08:39:00

标签: ruby-on-rails forms contact

尝试让一个联系表单处理一个已经存在一段时间的旧项目。我有我的模型,视图和控制器的联系人视图和以下内容。我的contact.html.erb文件还没有任何内容。谢谢你尽你所能的帮助。你真的可以帮我解决这个问题,而不是告诉我它有多“容易”吗?我一直在努力尝试这个并且没有在哪里。

型号:

    class MessagesController < ApplicationController

    def new
    @message = Message.new
  end

  def create
    @message = Message.new(message_params)

    if @message.valid?
      MessageMailer.new_message(@message).deliver
      redirect_to contact_path, notice: "Your messages has been sent."
    else
      flash[:alert] = "An error occurred while delivering this message."
      render :new
    end
  end

private



def message_params
    params.require(:message).permit(:name, :email, :content)
  end

end

查看:mailer.html.erb

 <html>
  <body>
    <%= yield %>
  </body>

控制器: messages_controller.rb

    class MessagesController < ApplicationController

    def new
    @message = Message.new
  end

  def create
    @message = Message.new(message_params)

    if @message.valid?
      MessageMailer.new_message(@message).deliver
      redirect_to contact_path, notice: "Your messages has been sent."
    else
      flash[:alert] = "An error occurred while delivering this message."
      render :new
    end
  end

private

  def message_params
    params.require(:message).permit(:name, :email, :content)
  end

end



Messages mailer:

    class MessageMailer < ApplicationMailer


    default from: "Your Mailer <noreply@domain.com>"
    default to: "Your Name <hello@domain.com>"

  def new_message(message)
    @message = message

    mail subject: "Message from #{message.name}"
  end


end

0 个答案:

没有答案