开发者

Filtering Questions (posts) by tag_name in TagController#index passing /tags?tag_name=something

I am trying to get my TagsController#index action to display only the Questions that contain certain tags when passing the search parameter tag_name. I am not getting an error just can't get the filter to work correctly such that upon receiving the /tags?tag_name=something url, only those questions are displayed . Here is the setup:

class Tag < ActiveRecord::Base

  belongs_to :question

   def self.search(str)
     return [] if str.blank?
     cond_text   = str.split.map{开发者_C百科|w| "tag_name LIKE ? "}.join(" OR ")
     cond_values = str.split.map{|w| "%#{w}%"}
     all(:order => "created_at DESC", :conditions =>  (str ? [cond_text, *cond_values] : []))
   end

end

and

class Question < ActiveRecord::Base
  has_many :tags, :dependent => :destroy
end

tag link that would send the URL to the TagsController looks like this:

<%= link_to(tag_name, tags_path(:tag_name => tag_name)) %>

and outputs:

/tags?tag_name=something

In my /views/tags/index.html.erb view I am calling the questions by rendering a partial /views/questions/_question.html.erb.

<%= render :partial => @questions %>

When I send the URL with the search parameter nothing happens. Here is my TagsController#index action:

def index
    @tags = Tag.search(params[:search]).paginate :page => params[:page], :per_page => 5
    @tagsearch = Tag.search(params[:search])
    @tag_counts = Tag.count(:group => :tag_name, 
       :order => 'count_all DESC', :limit => 100)

    @questions = Question.all( :order => 'created_at DESC', :limit => 50)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tags }
    end
  end

How can I properly filter questions displayed in the /views/tags/index.html.erb file for the parameter tag.tag_name?

UPDATE:

It appears that I have figured this out. I just had to replace:

<%= link_to(tag_name, tags_path(:tag_name => tag_name)) %> x<%=tag_count%>

with

<%= link_to(tag_name, tags_path(:search => tag_name)) %> x<%=tag_count%>


I believe that the issue is in the format of your SQL like clause, you need the format to be

%<query-here>%


It appears that I have figured this out. I just had to replace:

<%= link_to(tag_name, tags_path(:tag_name => tag_name)) %> x<%=tag_count%>

with

<%= link_to(tag_name, tags_path(:search => tag_name)) %> x<%=tag_count%>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜