Rails 3: Using a link_to :action on an "index", isn't working
edit: 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.
What this link_to is supposed to do:
When you press the link, it sorts the post index a certain way. One button sorts by trending value, another sorts by time, etc.
for some reason, in the 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 displayIcon(1), {:action => :make_feed_preference, :id => current_user.id,
:preference => "trending value", :controller => "users"}, :method=>:post %>
in the users controller:
def make_feed_preference
@user = User.find(params[:id])
@user.feed_preference = params[:preference]
@user.save
redirect_to '/posts'
end
in the view for the posts index:
def index
@user = current_user
if @user.feed_preference == nil
@user.feed_preference = "trending value"
end
@posts = Post
unless @user.tag_list == []
@posts = Post.tagged_with(@user.tag_list, :match_all => :true)
end
if @user.feed_preference == "trending value"
@posts = @posts.order('trending_value DESC').page(params[:page]).per(5)
elsif @user.feed_preference == "time"
@posts = @posts.order('created_at DESC').page(params[:page]).per(5)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
Routes.rb
resources :users do
resources :comments
resources :posts
member do
get :following, :followers
end
collection do
post "tag_with"
post "clear_tag_list_and_preferences"
post "make_feed_preference"
end
end
Whenever I check the values, it's as if clicking the link_to does nothing.
You say it does nothing? The form doesn't even post?
If it's not posting at all... do you have your link_to in a form? If you are using the post method you have to have it in a form unless you are using something like the ujs jQuery libraries which use jQuery / Javascript to help with your 'post' operation.
Using the gem : gem 'jquery-rails' https://github.com/indirect/jquery-rails
I would also like to see your 'rake routes'
精彩评论