开发者

Function called by link_to, doesn't work even when hard coded [duplicate]

开发者 https://www.devze.com 2023-04-01 13:15 出处:网络
This question already has answers here: Rails: attributes not being saved even though I called @user.save
This question already has answers here: Rails: attributes not being saved even though I called @user.save (2 answers) Closed 9 years ago.

For some reason, in my function make_feed_preference, the @user.feed_preference never changes, even if I hard 开发者_StackOverflowcode it to @user.feed_preference = "foobar" but it looks like redirect_to 'posts' is working fine.

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 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.


I would be using a conditional here:

redirect_to '/posts', :notice => if @user.save
    'You have updated your feed preference.'
else
    'Your input was invalid.'
end

You'll find that the @user validations are probably failing.

0

精彩评论

暂无评论...
验证码 换一张
取 消