开发者

How to use link_to to pass value back to a search field and execute in RAILS, RoR?

I have a search field and button. When the user searches something like "开发者_如何学JAVAcat" my view renders a bunch of related keywords such as "catnip", "cat toys", "cats" etc... each of these results are links, and are to pass the value of the string displayed back into the search to generate results for the selected link. For example:

  1. User renders search page and searches for "cat"
  2. view page renders results related to "cat" such as "catnip" "kittens"
  3. User now clicks on "catnip"
  4. View page renders results related to "catnip" such as "grass"

Is this possible with link_to? I'm lost and not quite sure what to do...

--

My Code:

SEARCH VIEW PAGE

<% form for(:search, url => search_path) do |f| %>
Search: <%= f.text_field :search %><br>
<%= f.submit "Search Keyword" %><p>   

<% unless @keywords.nil? %>
  <h3>Keyword Results</h3>
  <% @keywords.each do |k| %>
  <%= link_to k.name, :controller => "search", :action => "keywords", value => k.name %>
<% end %>

SEARCH CONTROLLER

def keywords
  if request.post? then
  @keywords = googlesearchapi(params[:search])
end

I want to pass the link_to value that the user clicks on as the :search parameter... thanks in advance for any advice~~


First of all, you want the link to be: "../search/keywords?search=catnip", to do this, modify the link_to as:

<%= link_to k.name, :controller => "search", :action => "keywords", :search => k.name %>

Then, you need to delete the line if request.post? then, otherwise it won't handle the requests coming from link_to, as it is a GET request (not POST).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜