Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I process request with parameters

mike zenon asked in Rails

i am new to ruby on rails, just run the examples https://guides.rubyonrails.org/getting_started.html#installing-rails

I just wonder if I want list all the articles with certain string in the title, how can I do it in rails?

Reply

Hey Mike,

You would take the param and then query the database with it in your SQL.

Something like:
Article.where("title LIKE ?", "%#{params[:query]}%")

Reply

Would you please provide me more details? Where should I add this query? I would like to display selected rows or all rows based on the parameter.

  • For GET articles?special=false, return all articles

  • For GET articles?special=true, return all articles that has "token" in the title

The followings are some source code related( That is my guess).

Thanks a lot, I am really appreciate your help!

bin/rails routes
articles GET /articles(.:format) articles#index

===app/controllers/articles_controller.rb

class ArticlesController < ApplicationController
def index
@articles = Article.all
end

=== app/views/articles/index.html.erb
<% @articles.each do |article| %>

<%= article.title %>
<%= article.text %>
<%= link_to 'Show', article_path(article) %>

<% end %>

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.