Rails |拯救模型中的异常并使用控制器中的错误
Rails | Rescue exception in model and use the error in controller
我正在使用 twitter gem 来允许用户从我的应用程序 post 推文。
这是我的 tweet.rb 文件
class Tweet < ActiveRecord::Base
belongs_to :user
validates :user_id, :tweet, presence: true
before_create :post_to_twitter
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
// I want to do a redirect_to root_path, notice: "Please fix the error #{error.message}"
// but its against MVC structure to pattern for a model to redirect, since its the task of a controller. How can I achieve this in controller
end
end
end
在post_to_twitter方法的救援块中,我想做一个redirect_to root_path, notice: "Please fix the error #{error.message}"
但它反对 MVC 结构来模式化模型重定向,因为它是控制器的任务。我怎样才能在控制器中实现这一点?
这是tweets_controller.rb个文件
class TweetsController < ApplicationController
def new
@tweet = Tweet.new
end
def create
@tweet = Tweet.new(tweet_params)
@tweet.user_id = current_user.id
if @tweet.save
redirect_to new_tweet_path, notice: "Your tweet has been successfully posted"
else
render 'new'
end
end
private
def tweet_params
params.require(:tweet).permit(:tweet, :user_id)
end
end
你可以在回调不成功时给对象添加错误:
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
# error will be appear in `@tweet.errors`
errors.add(:base, "Please fix the error #{error.message}")
false
end
end
然后,当 @tweet.save
returns false
时,在控制器中做任何你需要的事情(并且它将 return false 因为回调不成功):
def create
@tweet = Tweet.new(tweet_params)
@tweet.user_id = current_user.id
if @tweet.save
redirect_to new_tweet_path, notice: "Your tweet has been successfully posted"
else
# render 'new'
redirect_to root_path, notice: @tweet.errors.full_messages.join(',')
end
end
在控制器中使用rescue_from
来捕获错误:
class TweetsController
rescue_from Twitter::Error, with: :error
def error
redirect_to root_path, alert: "Please fix the error #{error.message}"
end
end
此外,如果您想同时捕获模型和控制器中的错误,您可以通过重新引发来实现:
class Tweet < ActiveRecord::Base
belongs_to :user
validates :user_id, :tweet, presence: true
before_create :post_to_twitter
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
self.twottered = false
raise error
end
end
end
我正在使用 twitter gem 来允许用户从我的应用程序 post 推文。
这是我的 tweet.rb 文件
class Tweet < ActiveRecord::Base
belongs_to :user
validates :user_id, :tweet, presence: true
before_create :post_to_twitter
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
// I want to do a redirect_to root_path, notice: "Please fix the error #{error.message}"
// but its against MVC structure to pattern for a model to redirect, since its the task of a controller. How can I achieve this in controller
end
end
end
在post_to_twitter方法的救援块中,我想做一个redirect_to root_path, notice: "Please fix the error #{error.message}"
但它反对 MVC 结构来模式化模型重定向,因为它是控制器的任务。我怎样才能在控制器中实现这一点?
这是tweets_controller.rb个文件
class TweetsController < ApplicationController
def new
@tweet = Tweet.new
end
def create
@tweet = Tweet.new(tweet_params)
@tweet.user_id = current_user.id
if @tweet.save
redirect_to new_tweet_path, notice: "Your tweet has been successfully posted"
else
render 'new'
end
end
private
def tweet_params
params.require(:tweet).permit(:tweet, :user_id)
end
end
你可以在回调不成功时给对象添加错误:
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
# error will be appear in `@tweet.errors`
errors.add(:base, "Please fix the error #{error.message}")
false
end
end
然后,当 @tweet.save
returns false
时,在控制器中做任何你需要的事情(并且它将 return false 因为回调不成功):
def create
@tweet = Tweet.new(tweet_params)
@tweet.user_id = current_user.id
if @tweet.save
redirect_to new_tweet_path, notice: "Your tweet has been successfully posted"
else
# render 'new'
redirect_to root_path, notice: @tweet.errors.full_messages.join(',')
end
end
在控制器中使用rescue_from
来捕获错误:
class TweetsController
rescue_from Twitter::Error, with: :error
def error
redirect_to root_path, alert: "Please fix the error #{error.message}"
end
end
此外,如果您想同时捕获模型和控制器中的错误,您可以通过重新引发来实现:
class Tweet < ActiveRecord::Base
belongs_to :user
validates :user_id, :tweet, presence: true
before_create :post_to_twitter
def post_to_twitter
begin
user.twitter.update(tweet)
rescue Twitter::Error => error
self.twottered = false
raise error
end
end
end