我这样做是否正确 - 使用 link_to 从控制器调用操作
请原谅这个简单性和长度,但我有一个小测试应用程序,它有一个用户表,其中包含姓名、电子邮件和薪水属性。我创建了一个邮件程序,该邮件程序将根据我的判断(换句话说,当我单击按钮时)向特定用户发送此数据的报告。我的按钮是使用 link_to 创建的,并调用我的主 users_controller 中的操作,然后该操作调用邮件程序操作。 (我希望这是有道理的)。它看起来如下所示,并且正如我所希望的那样工作;我只是想知道这是否是执行类似操作的正确方法:
在我的 users_controller (由脚手架生成器创建)中:
def sendemail
@user = User.find(params[:id])
UserMailer.welcome_email(@user).deliver
redirect_to user_path(@user)
flash[:notice] = 'Email has been sent!'
end
在 user_mailer.rb 文件中:
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
在用户的 show.html.erb 页面中,这就是如何电子邮件被发送:
<%= link_to "Send Email", sendemail_user_path(@user) %>
在我的routes.rb文件中,以便一切正常执行(确实如此):
resources :users do
member do
get 'sendemail'
end
所以,说了这么多,它就像它应该的那样工作。我单击用户的 show.html.erb 页面,其中我将获得我想要最终显示的数据和图表,并且根据我的判断,我可以向该用户发送一封包含此数据的电子邮件,或者我在其中输入的任何内容mailer.html.erb 文件。当它被发送时,它会闪烁我在控制器中指定的消息,并将我留在该页面上,就像我指定的那样;所以它正在工作。我只是想知道,这是正确且最 Ruby/railsy 的做事方式吗?
Please excuse the simplicity and length of this, but I have a little test app that has a users table, with name, email, and salary attributes. I created a mailer that will send out a report of this data to a specific user at my discretion, in other words, when I click a button. My button is created using link_to, and calls an action in my main users_controller, which then calls the mailer action. (I hope that just made sense). It looks like the following and is working just as I hoped; I'm just looking to know if this is the right way to do something like this:
In my users_controller (created by the scaffold generator):
def sendemail
@user = User.find(params[:id])
UserMailer.welcome_email(@user).deliver
redirect_to user_path(@user)
flash[:notice] = 'Email has been sent!'
end
In the user_mailer.rb file:
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
In the User's show.html.erb page, this is how the email gets sent:
<%= link_to "Send Email", sendemail_user_path(@user) %>
In my routes.rb file, so that everything executes properly (which it does):
resources :users do
member do
get 'sendemail'
end
So, having said all this, it works just like it should. I click on the user's show.html.erb page where I would have the data and charts that I want to eventually display, and at my discretion, I can kick out an email to that user with this data, or whatever I put in the mailer.html.erb file. When it is sent, it flashes the message I specified in the controller, and leaves me on that page, just as I specified; so it's working. I just want to know, is this the right and most ruby/railsy way to do things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码看起来与 Rails Guides Action Mailer 示例非常相似,因此只要说您正在创建铁路代码。
此外,如果您的应用程序发展到更大的规模,您可能需要考虑通过后台作业发送电子邮件,以避免电子邮件发送阻塞当前线程。
否则代码看起来很棒。当然,您很可能会在控制器中成功执行某些操作后发送电子邮件,而不是直接执行专门的电子邮件发送操作。例如,您可能有一个
welcome
操作,该操作会在成功保存用户记录时发送电子邮件。This code looks very similar to the Rails Guides Action Mailer example, so suffice it to say you are creating railsy code.
Also, if your application evolved into a much grander scale you would want to consider delivering emails via a background job to avoid email deliveries from blocking the current thread.
Otherwise the code looks great. Of course, you would most likely send an email after doing something successful in a controller, rather than have a dedicated action for emailing directly. For instance, you might have a
welcome
action that sends an email on a successful user record save.