我有一个应用程序,其中: 1.用户在查看其个人资料信息的页面上 2.用户按下按钮,通过此页面向某人发送电子邮件 3.在发送电子邮件之后,用户将被再次发送回查看他们的个人资料信息,并且会发出通知,告诉他们电子邮件是否有效。
我没有。 3.我不确定如何设置重定向(或其他适当的选项),以便用户再次查看其个人资料信息
控制器:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy, :email]
# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.new
end
# GET /profiles/1/edit
def edit
@profile = Profile.find_by user_id: current_user.id
end
# POST /profiles
# POST /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
def email_profile
destination = params[:to]
share = Share.profile(@profile, destination)
if destination =~ /@/ && share.deliver
redirect_to @profile, notice: 'email sent'
else
redirect_to @profile, notice: 'email failed'
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end
end
分享梅勒:
class Share < ActionMailer::Base
default_url_options[:host] = "localhost:3000"
default from: "from@example.com"
def profile(profile, destination)
@profile = profile
mail(to: destination, subject: "sent you stuff")
end
end
当前错误:
ActionController::ActionControllerError in ProfilesController#email_profile
Cannot redirect to nil!
我认为它与发送电子邮件后没有传递的id参数有关...但我是新手,所以我真的不知道我在说什么..感谢任何指导所以我可以解决这个问题并且更好地理解ROR:)
答案 0 :(得分:1)
您可能需要先找到@profile
。我猜想Profile.find(params[:profile_id])
之类的东西不见了。