【发布时间】:2012-05-01 12:24:47
【问题描述】:
如何将数据从控制器传递到模型?
在我的application_controller 中,我获取用户的位置(州和城市)并包含一个before_filter 以使其在我的所有控制器中都可以通过
before_filter :community
def community
@city = request.location.city
@state = request.location.state
@community = @city+@state
end
然后我尝试通过以下方式将控制器中检索到的数据添加到模型中:
before_save :add_community
def add_community
self.community = @community
end
但是,数据永远不会从控制器传输到模型。如果我使用:
def add_community
@city = request.location.city
@state = request.location.state
@community = @city+@state
self.community = @community
end
request.location.city 和 request.location.state 方法在模型中不起作用。我知道其他一切都在工作,因为如果我将@city 和@state 定义为字符串,在def_community 下,那么一切正常,除了我没有动态变量,只是放置在模型中的字符串。另外,我知道请求在控制器/视图中工作,因为我可以让它们显示正确的动态信息。问题只是将数据从控制器获取到模型。非常感谢您的宝贵时间。
【问题讨论】:
-
我建议您删除此问题或您的其他问题,因为它们本质上是相同的。下一次,如果你想让事情更清楚,你可以编辑你原来的问题:stackoverflow.com/questions/10236645/…
-
欢迎来到 StackOverflow!请记住对您认为有用的所有答案进行投票,包括对他人问题的答案。 “检查”(选择)您问题的最佳答案。
标签: ruby-on-rails ruby ruby-on-rails-3 model-view-controller model