[英]Custom method redirect in Rails
I've created a custom method in my model, which finds a record by name: 我在模型中创建了一个自定义方法,该方法按名称查找记录:
def find_city
Place.find_by_name(city_name)
end
I can call this method in my view with place_path(@place.find_city)
, which works great when a place with the appropriate name exists. 我可以在我的视图中使用
place_path(@place.find_city)
调用此方法,当存在具有适当名称的地方时,该方法非常place_path(@place.find_city)
。 What I would like to be able to do is write in a redirect for when the place doesn't exist, and I'm unsure about where the logic should go. 我想做的是在该地方不存在时以重定向方式编写,而我不确定逻辑应该去哪里。 Basically, I would like to write something like:
基本上,我想写一些类似的东西:
respond_to do |format|
if @place.find_city.blank?
format.html { redirect_to :action => "new" }
else
format.html { render :action => "show" }
end
end
...but I would still like the controller to respond to place_path(@place)
in the usual manner as well. ...但是我仍然希望控制器也以通常的方式响应
place_path(@place)
。 Any help would be much appreciated! 任何帮助将非常感激!
EDIT: sorry for the confusion, should have explained my example further. 编辑:抱歉给您带来的困惑,应该进一步解释我的例子。 I have a Place model that has both 'city_name' and 'name' as attributes.
我有一个同时具有“ city_name”和“ name”作为属性的Place模型。 The
find_city
custom method that I detailed above finds the place whose name matches the city_name for another place eg. 我上面详细介绍的
find_city
自定义方法会找到名称与另一个地方的city_name相匹配的地方,例如。
Place.name = "foo"
Place.city_name = "baz"
So therefore Place.find_city
gives the record where Place.name = "baz"
. 因此,
Place.find_city
给出了其中Place.name = "baz"
的记录。 Cheers! 干杯!
Do something like this 做这样的事情
keep your model as 保持模型为
class City < ActiveRecord::Base
named_scope :by_name, lambda {|city|{:conditions => "name=#{city}"}}
end
in your controller 在您的控制器中
class CitiesController < ApplicationController
def index
@city = City.by_name(<city name>)
if @city.nil?
<redirect to one place>
else
<redirect to another place>
end
end
end
and in your view access the @city parameter. 并在您的视图中访问@city参数。
** Normally we shouldnt access Models directly from views. **通常,我们不应该直接从视图访问模型。 Either we should do it through controllers or helpers
我们应该通过控制器或助手来做到这一点
Hope this helps 希望这可以帮助
cheers 干杯
sameera 萨梅拉
I've decided to create a helper method for this problem - what I've described above might seem a bit of an unorthodox approach, but I only need the find_city
method to create a links bar for each place, so I don't think I really need to create a separate city model, or define a formal self-referential relationship. 我决定为这个问题创建一个辅助方法-我上面描述的内容似乎有点
find_city
,但是我只需要find_city
方法为每个地方创建一个链接栏,所以我认为我确实需要创建一个单独的城市模型,或者定义一个正式的自我参照关系。 Just in case anyone might find this useful, I've created a links_bar
helper: 万一有人发现它有用,以防万一,我创建了一个
links_bar
帮助器:
module PlacesHelper
def links_bar(place)
if place.city_name.blank?
render "no_city_links"
elsif place.find_city.nil?
render "nil_find_city_links"
else
render "complete_links"
end
end
end
Each partial then has the required behaviour depending upon whether or not the find_city
method returns a record or not. 然后,每个部分都有所需的行为,具体取决于
find_city
方法是否返回记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.