rails4 - 格式错误的字符串 [英] rails4 - malformed format string

查看:34
本文介绍了rails4 - 格式错误的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要,请随意索取更多代码,因为我不确定我错在哪里...非常感谢问候

解决方案

我修好了!

在我的控制器教程中,我这样做了:非常感谢芬达带我走对了路!

def 索引#binding.pry如果 params[:search].present?@tutos = Tuto.search(params[:search]).includes(:user, :category)别的@tutos = Tuto.all.includes(:user, :category)结尾如果 params[:filter].present?@tutos = Tuto.joins(:category).where('categories.name LIKE ?', params[:filter])别的@categories = Category.all结尾结尾

After the correction of a wrong syntax I have a new error and I am not sure where it comes from:

categories were created in the console

 Category Load (2.0ms)  SELECT "categories".* FROM "categories"
 => #<ActiveRecord::Relation [#<Category id: 1, name: "Ruby", created_at: "2016-09-26 09:03:17", updated_at: "2016-09-26 09:03:17">, #<Category id: 2, name: "Rails4", created_at: "2016-09-26 09:03:25", updated_at: "2016-09-27 14:32:39">, #<Category id: 3, name: "Rails5", created_at: "2016-09-26 09:03:30", updated_at: "2016-09-27 14:35:25">, #<Category id: 4, name: "Heroku", created_at: "2016-09-26 09:03:35", updated_at: "2016-09-27 14:35:47">, #<Category id: 5, name: "AWS-Amazon", created_at: "2016-09-26 09:03:43", updated_at: "2016-09-26 09:03:43">]>

When I select:

  • "Ruby" I have: malformed format string - %R
  • "Rails4" I have: malformed format string - %R
  • "Rails5" I have: malformed format string - %R
  • "Heroku" I have: malformed format string - %H
  • "AWS-Amazon" I have: too few arguments

I am trying to filter my categories by name:

category model

class Category < ActiveRecord::Base
  has_many :tutos

  def self.filter(filter)
    if filter
      where(["name LIKE '%#{filter}%'"]).order('created_at DESC')
    else
      all
    end
  end
end

1st Edit

I've tried different way but I have issues in each:

`where(["name LIKE '%#{filter}%'"]).order('created_at DESC')` 

returns the malformed string alert

`where("name LIKE '%#{filter}%'").order(created_at: :desc)` 

and

`where("name LIKE ?", "%#{filter}%").order('created_at DESC')`

return all the categories without filtering....

you may want to see the tutos/views/index

.container
  .row
    h1.text-gray Tutorials 
  .row.search_banner
    .col-xs-3
      =form_tag tutos_path, :method => 'get' do 
        =text_field_tag :search, params[:search],  placeholder:"Search by keywords"
        =submit_tag "Search", class:'btn btn-xs btn-default btn-search'

    .col-xs-3
      =form_tag tutos_path, :method => 'get' do 
        =select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter])
        =submit_tag "Search", class:"btn btn-xs btn-default btn-search"

    .col-xs-3
      -if user_signed_in?
        = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"
      br
      br    


#tutos.transitions-enabled
  -@tutos.each do |tuto|
    .box.panel-default
      = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)


      h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"     
      h6 
        | Created by:
        span<>
        = tuto.user.full_name
      br
      span.glyphicon.glyphicon-heart
      span<>
      = tuto.get_upvotes.size
      br
      br

2nd edit

Rails console log : if I search for "Heroku" Cartegory: but as my categories belongs to tutos... (Maybe have a look on the website filter are not online yet though but you will see the idea)

Should I do something like this
Tuto.category.where("name LIKE ?", "%#{filter}%").order('created_at DESC')

Started GET "/tutos?utf8=%E2%9C%93&filter=Heroku&commit=Search" for ::1 at 2016-09-28 17:50:41 +0200
Processing by TutosController#index as HTML
  Parameters: {"utf8"=>"✓", "filter"=>"Heroku", "commit"=>"Search"}
  Category Load (0.3ms)  SELECT "categories".* FROM "categories"
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Tuto Load (0.1ms)  SELECT "tutos".* FROM "tutos"
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (1)
  Category Load (0.1ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (2, 3, 5, 4, 1)
   (0.1ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 16], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 17], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 18], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 19], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 20], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 21], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 22], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 23], ["votable_type", "Tuto"], ["vote_flag", "t"]]
   (0.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 24], ["votable_type", "Tuto"], ["vote_flag", "t"]]
  Rendered tutos/index.html.slim within layouts/application (29.0ms)
  Rendered layouts/_nav.html.slim (0.4ms)
Completed 200 OK in 104ms (Views: 94.1ms | ActiveRecord: 1.9ms)

3rd Edit

tuto model

class Tuto < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
  belongs_to :category
  validates :category_id, presence: true


  def self.search(search)
    if search
     where(["title LIKE ?","%#{search}%"]).order('created_at DESC')
    else
      all
    end
  end
end

category model I've build my category filter like the tuto search

class Category < ActiveRecord::Base
  has_many :tutos

  def self.filter(filter)
    if filter
      #where(["name LIKE '%#{filter}%'"]).order('created_at DESC')
      #where("name LIKE '%#{filter}%'").order(created_at: :desc)
     where("name LIKE ?", "%#{filter}%").order('created_at DESC')
    else
      all
    end
  end
end

Checked with the suggested raise 'check'

Feel free to ask for more code if needed, as I am not sure where I am wrong... Many thanks regards

解决方案

I fixed it !

In my controller tutos I did this: many thanks to fanta who took me on the right way !

def index
  #binding.pry
    if params[:search].present?
      @tutos = Tuto.search(params[:search]).includes(:user, :category)
    else
      @tutos = Tuto.all.includes(:user, :category)
    end

    if params[:filter].present?
      @tutos = Tuto.joins(:category).where('categories.name LIKE ?', params[:filter])
    else
      @categories = Category.all
    end
  end

这篇关于rails4 - 格式错误的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆