English 中文(简体)
铁路通过参数进入网页,根据这些参数使用不同的方法
原标题:Rails passing parameters to pages and calling different methods based on them

我继续讨论我提出的新问题,由于你的耐心,下面可能有一个问题。

我想要做到的是:

我有一个称为搜索.html.erb的模板(概览),其中我希望根据用户投入使用不同的方法,具体来说,在前一页通过链接获得变量。 因此,如果用户点击“commas”链路,就会通过类似的东西。

search?type=commas

(由于我理解,在铁路中保留行动以替代在主计长内部宣布的方法) 路线档案有:

  match  :controller/:action/:type/ 

允许我做些什么是“http:// localhost:3000/actors/search/commas”,作为穿透mas类的方法?

然后,搜索页将使用《债权人模式》中的显示商业法:

def self.showCommas
    @commas = self.where("actor LIKE ?", "%,%,%")
end

如果他们点击“赤贫”,就会通过

search?type=acute

该网页将称为《债权人模式》中的急性方法。

Finally I also want them to be able to pass a search term and the search page would do a SQL query with that term using .find_by_sql.

这是在公共卫生和社会福利部中实施的三边工作,我刚刚使用过一种设备。

if ($_GET[action] ==  commas ) { ...

但是,如何在铁路运输? 问题 我有:

如果我在监察员控制员中为搜查规定一个方法:

def search(term)    
    Actor.find_by_sql([sql query here with  %#{term}% ]) 
end

铁路会抱怨,我对控制器中的一种方法有太多的论点。 因此,我把我的所有方法移至《示范公约》(认为这是这样做的正确方式?)

将这些内容输入我的搜索网页:

if type.nil?
    criteria = "No type specified"
elsif type ==  commas  
    criteria = type
    @actors = Actor.showCommas
end

紧接着,我对未界定的类型变量有了一个错误:没有界定的地方变数或方法`类型'。

因此,我做了大量的事情,但像铁路一样,从铁路运输公司到铁路运输站,很难把你头 wrap在铁路运输途中。 我理解,以你的行动和铁路一样的名称来看待这一问题的主要理念,但我不想为我需要做的每一个问题(或其它一点)建立一个网页。 此外,我所读的书似乎没有详细论述这一点,同样,由于你帮助和纵容在一个职位上提出多个问题。

最佳回答

Hard to fully answer this question as a little info is missing but hopefully this steers you in the right direction. Your search?type=commas url will route to the actors controller where by the looks of it you have a search method. The type=commas will be accessible in the params hash (params[:type]).

在其最佳做法中,将逻辑推向模式,就像你所能做的那样。

def search  
    if params[:type] 
        if params[:type] == "commas"
            ... search logic
        elsif
            ... other search logic
        end 
    end
end

In Rails the controller method using you naming convention would look more like this;

def search 
    @actors = Actor.search(params[:type])
end

注:params [:type],以获取您的类型参数。 你写这封信的方式是作为控制器方法的争论,因为控制器方法没有争论。

模式可以看一看类似情况;

def self.search(type)
    case type
    when "commas"
      Actor.where(:type => "commas")
    when "whatever"
      Actor.where(:type => "whatever")
    else
      Actor.all
    end
end

你的搜索方法显然大不相同,但这只是如何将 para子从 right权传至模型的正确例子。

问题回答

在模型中未这样做,而是在控制器中。 您自动获得GET参数(如PHP)。 它是在暗中。 例如,你可以就案件提出诉讼:

def search
  case params[:type]
  when  commas 
    search_with_commas
  when  acute 
    search_with_acute
  ...
  else
    default_search
  end
end

private
  def search_with_commas
     ...
  end

  ...

当然,如果你只需要修改询问,那么你就只能从何时处理一件事。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签