以HAML格式创建搜索栏

时间:2016-04-28 00:56:32

标签: ruby haml erb

这个教程代码是ERB格式,但我在HAML中这样做。

ERB代码:

if [[ $(ls /some/dir/) ]]; then echo "huzzah"; fi;

我现在在HAML中有什么:

<%= form_tag search_movies_path, method: :get, class: "navbar-form navbar-right", role: "search" do %>
  <p>
    <%= text_field_tag :search, params[:search], class: "form-control" %>
    <%= submit_tag "Search", name: nil, class: "btn btn-default" %>

但它不起作用。有谁知道怎么做?

我的搜索方法如下:

- if user_signed_in?
  = link_to current_user.name, edit_user_registration_path
  = link_to "Sign Out", destroy_user_session_path, method: :delete
  = link_to "Selling", new_post_path, class: "button"
  = form_tag posts_path, :method => :get do
    = text_field_tag :search, params[:search] 
    = submit_tag "Search", name: nil, class: "btn btn-default"
- else 
  = link_to "Sign in", new_user_session_path
  = link_to "Sign Up", new_user_registration_path
  = link_to "Selling", new_post_path, class: "button" 
  = form_tag posts_path, :method => :get do
    = text_field_tag :search, params[:search]
    = submit_tag "Search", name: nil, class: "btn btn-default"

1 个答案:

答案 0 :(得分:0)

您已定义了一个名为PostsController的动作(大概在search中),但您引用了posts_pathindex将转到index行动。您需要将代码放入search_posts_path或使用search_posts_path

这里是您发布的ERB的等效HAML,使用= form_tag search_posts_path, method: :get, class: "navbar-form navbar-right", role: "search" do %p = text_field_tag :search, params[:search], class: "form-control" = submit_tag "Search", name: nil, class: "btn btn-default" 来点击您定义的操作:

SELECT `u`.`id` AS user_id, username,
(SELECT COUNT(message_user.message_read)  
FROM message_user  
WHERE message_user.message_read = 0 
AND sent_id = user_id  
AND receive_id = {$userId}) as unread  

FROM `user` AS `u`
LEFT JOIN `message_user` AS `mu` 
ON 
    (CASE WHEN `u`.`id` != {$userId}
        THEN `u`.`id` = `mu`.`sent_id`
        WHEN `mu`.`sent_id` = {$userId} AND `mu`.`receive_id` = {$userId}
        THEN `u`.`id` = `mu`.`sent_id`
    END)
OR  
    (CASE WHEN `u`.`id` != {$userId}
        THEN `u`.`id` = `mu`.`receive_id`
    END)

LEFT JOIN `message` AS `m` ON `m`.`id` = `mu`.`message_id`

WHERE u.id IN  
(SELECT contact_id FROM allowed_contact WHERE user_id = {$userId})
GROUP BY u.id
ORDER BY MAX(`m`.`timestamp`) DESC;