Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter In ActionController?

I have problem in my controller page. Btw i want to execute localhost:3000/article/donat?author_id=4, this mean i want to get on view only article with author_id = 4 i have tried type code like this.

def donat
    @title = "All blog entries"
    if params[:author_id] == :author_id
      @articles = Article.published.find_by_params(author_id)
    else
      @articles = Article.published
    end
    @articles = @articles.paginate :page => params[:page], :per_page => 20
    render :template => 'home/index'
  end

It is not working. Have you any suggestion for this case?

like image 633
Ahmad Ramdani Avatar asked May 30 '26 21:05

Ahmad Ramdani


1 Answers

You want nested resources for this, and the getting started guide is a pretty good example of this.

Personally, I would put this at the top of my controller:

before_filter :find_author

And this at the bottom:

private 
  def find_author
     @author = Author.find(params[:author_id]) if params[:author_id]
     @articles = @author ? @author.articles : Article
  end

And then further up in the controller where I need to find articles:

@articles.find(params[:id])

Which will scope it appropriately.

like image 196
Ryan Bigg Avatar answered Jun 02 '26 21:06

Ryan Bigg