Save 36% for Black Friday! Learn more

Simon P

Joined

2,170 Experience
0 Lessons Completed
2 Questions Solved

Activity

Posted in Link to a method in my model

Hi

I have a method called Search(search) in my model which is called from my controller. This works fine when I enter a search term in an input box in my view.

What I now want to do is create a link for a preset search term that pass the preset search term to the method.

Something like:

<%= link_to "Search Term", articles_search_path("Search Term") %>

How should this link look and what do I need in my controller to pass this to my model?

Thanks

Simon

Posted in Using will_paginate and .limit(x) together.

Thanks Chris

I did have a play with total_count but couldnt quite get it right.

Much appreciated,

Posted in Using will_paginate and .limit(x) together.

Hi Chris

Thanks for your continued input.

My problem was that I wanted to display "Here are the last 150 articles" but the view has other options when it might show less or more articles so 150 was populated by @articles.count. The problem is when you use @articles.count on a paginated active record set it returns the whole table count rather than the number you limited the pagination set to.

My work around was to create a method called getarticlecount.

Which does this:

def getarticlecount(view)
    if view == "150"
      return "150"
    else
      return @articles.count
    end
  end

Made accessible to the view by adding this in the controller:

helper_method :getarticlecount

Rendered in the view by this:

<%= pluralize(getarticlecount(params[:view]), "result") %>

Not sure if the breaks any rails or ruby best practises but it does the job! Open to refactoring.

Simon

Posted in Using will_paginate and .limit(x) together.

Thanks Chris

I want the 150 most recent Articles paginated in 5 pages of 30.

I have managed a workaround but would love to understand how I could accomplish the original problem.

Posted in Using will_paginate and .limit(x) together.

Hi

It appears that will_paginate and .limit(x) will not work together. So this didn't work:

(I am trying to get 150 most recent records and paginate them)

@articles = Article.where.not(site_id: 'HIDE')
.limit(150)
.order('articles.created_at DESC')
.paginate(:page => params[:page], :per_page => 30)

But this does work:

(using the total_entries method of will_paginate)

@articles = Article.where.not(site_id: 'HIDE')
.order('articles.created_at DESC')
.paginate(:page => params[:page], :per_page => 30,total_entries: 150)

BUT @articles still pulls all 4500 records which isnt very efficient and also @articles.count returns 4500 rather than 150.

Is there away I can just get the 150 records from the DB and paginate them?

Thanks to craftycanuck in the Slack channel for helping me get this far,

Simon

Thanks, will give that a try.

Posted in CMS in Rails

Also google for the Jumpstart tutorials blogger app. It gives you a more feature rich blog tool compared with the more basic rails guide version.

Hi

As the question says.

Thanks

Simon

Posted in Integrating a blog within a Rails app.

Temptation is always to build something but that means time and money. I remembered this blog post that may give you some ideas - https://robots.thoughtbot.com/adding-a-blog-to-a-rails-app

Posted in Best way to build a Contact Us page

Thanks, will give it a try.

Posted in Best way to build a Contact Us page

Hi

First post so Hello World! Really enjoying this site.

I was just wondering what people consider to be the best way to build a Contact Us page.

I don't need to store the entered data, just email it to a couple of people.

I would want validation on the input.

I found this tutorial - is this broadly the correct approach?

https://www.murdo.ch/blog/build-a-contact-form-with-ruby-on-rails-part-1

Thanks

Simon

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.