link_to 调用的函数,即使在硬编码时也不起作用 [英] Function called by link_to, doesn't work even when hard coded
问题描述
出于某种原因,在我的函数 make_feed_preference
中,@user.feed_preference
永远不会改变,即使我将其硬编码为 @user.feed_preference = "foobar"
但看起来 redirect_to 'posts'
工作正常.
For some reason, in my function make_feed_preference
, the @user.feed_preference
never changes, even if I hard code it to @user.feed_preference = "foobar"
but it looks like redirect_to 'posts'
is working fine.
错误是即使在按下 link_to
之后 feed_preference
总是 nil
,所以 (if feed_preference ==nil
) 总是 true
.我仍然不知道是什么原因造成的.
The bug is that the feed_preference
is always nil
even after the link_to
is pressed, so the (if feed_preference == nil
) is always true
. I still don't know what's causing this.
make_feed_preference
存储在 users_controller 中.它的作用是显示新闻提要,但根据偏好值(例如,时间"、趋势值"、关注者")进行排序
make_feed_preference
is stored in the users_controller. What it does is shows a news feed, but sorted according to a preference value (e.g., "time", "trending_value", "followers")
<%= link_to displayIcon(1), {:action => :make_feed_preference, :id => current_user.id, :preference => "trending value", :controller => "users"}, :method => :post %>
<小时>
def make_feed_preference
@user = User.find(params[:id])
@user.feed_preference = params[:preference]
@user.save
redirect_to '/posts'
end
<小时>
注意:我通过向用户附加一个新类来修复它.
Note: i fixed it by attaching a new class to the users.
推荐答案
我会在这里使用条件:
redirect_to '/posts', :notice => if @user.save
'You have updated your feed preference.'
else
'Your input was invalid.'
end
您会发现@user 验证可能失败了.
You'll find that the @user validations are probably failing.
这篇关于link_to 调用的函数,即使在硬编码时也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!