无法通过json方法获得param

时间:2014-04-23 07:28:05

标签: ruby-on-rails

http://localhost:3000/users.json?q=lala

我希望通过发送一些字符串来获取json,

但我无法在params [:q]

中获得预期的字符串'lala'
p(params) `{"action"=>"index", "controller"=>"users", "locale"=>"en"}`

controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    # @users = User.all
    @users = User.order(:name)
    (1..100).each {p(params)}

    respond_to do |format|
      format.html
      format.json {render json: @users.where("name like ?", "%#{params[:q]}%")}
    end

  end

这是我的route.rb

  scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
    resources :articles
    resources :users
    root to: 'articles#index'
  end
  match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }, :via => [:get]
  match '', to: redirect("/#{I18n.default_locale}"), :via => [:get]
  match '', to: redirect("/#{I18n.default_locale}"), :via => [:get]

1 个答案:

答案 0 :(得分:0)

如果您将resources :users放在scope ":locale"之外,那么路线会生成正常路线,例如

GET     /users  users#index     users_path

默认情况下会创建RESTful路由。

请参阅this