简体   繁体   English

没有路由匹配{:action =>“ search”,:controller =>“ devise / index”}

[英]No route matches {:action=>“search”, :controller=>“devise/index”}

I'm having a problem with my research routes. 我的研究路线有问题。 they accuse this error: 他们指责此错误:

ActionController::UrlGenerationError in Devise::Sessions#new
No route matches {:action=>"search", :controller=>"devise/index"}

my application: 我的应用程序:

<!DOCTYPE html>
<html>
<head>
  <title>Javendi</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

    <div class = 'search', style="display:none">
        <%= form_for root_path, :url => {:action => 'search', :controller=>"index"} do |f|%>
           <%= text_field_tag "ad[price_min]", @ads_min, :placeholder => 'Price min', class: 'price' %>
           <%= text_field_tag "ad[price_max]", @ads_max, :placeholder => 'Price max', class: 'price' %><br>
           <%= text_field_tag "ad[title]", @ads_text, :placeholder => 'Ad name', class: 'titlesearch' %><br>
           <div class='categorysearch'>
           <%= collection_select :ad, :category_id, Category.all,:id,:name,:prompt => true, :selected => @ads_category_id %>
           <br></div>

           <%= f.submit'Searc', class: 'bottomsearch' %>
    <%end%>
    </div>

    <div class="cima">
        <!-- <a href="https://www.google.com.br/?gfe_rd=cr&ei=hnNOVNvJNYim8Aab2IHoCw&gws_rd=ssl"><img src="assets/2.gif" border="0" onmouseover="this.src='assets/7.gif'" onmouseout="this.src='assets/2.gif'"></a> -->

    <div class= 'logout'>
            <% if user_signed_in? %>
                <%= link_to image_tag('exit.png', width: '50px', height:'50px'),destroy_user_session_path, method: :delete %>
            <%else%>
            <%end%>
    </div>

        <div class= "home">
            <%= link_to image_tag('logo.png'),root_path %>
        </div>

        <div class="javendi">
            <% if user_signed_in? %>
                <button class= 'botton1'> 
                    <%= link_to 'Editar Perfil', edit_user_registration_path %>|
                    <%= link_to 'Novo Anúncio',  new_ad_path %>|
                    <%= link_to 'Meus Anúncios', my_ads_path %>|
                    <%= link_to 'Meus Favoritos', fav_ads_path %>
                </button>
            <%else%>
            <button class= 'botton'>
                <%= link_to 'Cadastre-se', new_user_registration_path %> |
                <%= link_to 'Login', new_user_session_path %>
            </button>
            <%end%>

            <div class='triangule'>
                <div class="seta-cima">
                </div>
            </div>
        </div>

        <% if user_signed_in? %>
            <div id='iconsearh2'>
                <%= image_tag("search.png") %>
            </div>
        <%else%>
            <div id='iconsearh'>
                <%= image_tag("search.png") %>
            </div>
        <%end%>
    </div>

    <script type="text/javascript">
    $('#iconsearh').click(function(){
        if($('.search').is(':visible')){
        $('.search').slideUp('fast')
        $('.seta-cima').css("display","none");
    }else{
        $('.search').slideDown('fast')
        $('.seta-cima').show();

    }
    });
    </script>

    <script type="text/javascript">
    $('#iconsearh2').click(function(){
        if($('.search').is(':visible')){
        $('.search').slideUp('fast')
        $('.seta-cima').css("display","none");
    }else{
        $('.search').slideDown('fast')
        $('.seta-cima').show();
    }
    });
    </script>

    <div class='cima2'>
        <div class='welcome'>
            <% if current_user.present?%>
                welcome <%=current_user.name%>
            <%end%>
        </div>
        <div class= 'createad'>
            <%= image_tag('7.gif', width: '50px', height:'50px')%>
        </div>
    </div>

        <div class="results">
            <%= yield %>
        </div>
  <div class= "bot">
    &nbsp
  </div>
</div>
</body>
</html>

my ads model: 我的广告模型:

class Ad < ActiveRecord::Base
  validates_presence_of :title, message: "deve ser preenchido"
  validates_presence_of :price, message: "deve ser preenchido"
  validates_presence_of :adress, message: "deve ser preenchido"
  validates_presence_of :email, message: "deve ser preenchido"
  validates_presence_of :phone, message: "deve ser preenchido" 
  belongs_to :user
  belongs_to :category
  has_many :photos, :dependent => :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
  has_many :user_ad_favs

  def can_edit?(current_user)
    if current_user.present?
      return current_user.id == self.user_id
    end
  end


  def self.search(query)
        category = query[:category_id].present? ? "category_id = #{query[:category_id]}" : nil
        title = query[:title].present? ? "title LIKE '%#{query[:title]}%'" : nil
        price_min = query[:price_min].present? ? "price >= #{query[:price_min].to_f}" : nil
        price_max = query[:price_max].present? ? "price <= #{query[:price_max].to_f}" : nil

        query = [category, title, price_min, price_max].compact.join(" AND ")

       return Ad.where ( query )
       end

       def user_fav(user)
        return self.user_ad_favs.find_by_user_id_and_ad_id(user.id, self.id)
       end
    end

my ads controller: 我的广告控制器:

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy, :fav_ad,:unfav_ad, :search,]

  # GET /ads
  # GET /ads.json
  def index
    @ads = Ad.all
  end

  # GET /ads/1
  # GET /ads/1.json
  def show
  end

  # GET /ads/new
  def new
    @ad = current_user.ads.build
    1.times { @ad.photos.build}
  end

  # GET /ads/1/edit
  def edit
  end

  def search
    @ads_min = params[:ad][:price_min]
    @ads_max = params[:ad][:price_max]
    @ads_title = params[:ad][:title]
    @ads_category_id = params[:ad][:category_id]

    @ads = Ad.search(params[:ad])
    render :action => 'index'
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = current_user.ads.build(ad_params)

    respond_to do |format|
      if @ad.save
        format.html { redirect_to @ad, notice: 'Ad was successfully created.' }
        format.json { render :show, status: :created, location: @ad }
      else
        format.html { render :new }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { render :show, status: :ok, location: @ad }
      else
        format.html { render :edit }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
    @ad.destroy
    respond_to do |format|
      format.html { redirect_to ads_url, notice: 'Ad was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def fav_ad
    user_ad_fav = @ad.user_fav(current_user)
    if user_ad_fav.present?
       user_ad_fav.update_attribute(:fav, true)
    else
      @ad.user_ad_favs.create(:user_id => current_user.id,:ad_id => @ad.id,:fav => true)
    end
    respond_to do |format|
      format.js {render inline: "location.reload();"}
    end
  end

  def unfav_ad
    @ad.user_fav(current_user).update_attribute(:fav, false) if @ad.user_fav(current_user).present?
    respond_to do |format|
      format.js {render inline: "location.reload();"}
    end
  end



  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ad
      @ad = Ad.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :price, :adress, :city, :state, :description, :email, :phone, :phone_type, :category_id, :photos_attributes =>[:photo])
    end
end

You made a mistake in the :controller parameter in your form_for : 您在form_for:controller参数中犯了一个错误:

<%= form_for root_path, :url => {:action => 'search', :controller=>"index"} do |f|%>

First of all, the :controller should be 'ads' since the search action is in the AdsController you provided a sample of. 首先, :controller应该是'ads'因为search动作在您提供的示例AdsController中。

Second, you're using form_for wrong. 其次,您使用form_for错误。 You're passing in two different paths, root_path and the :url hash. 您要传递两个不同的路径, root_path:url哈希。 The first parameter should be the model you're trying to make a form for. 第一个参数应该是您要为其创建表单的模型。 But you don't need that here, you just want a search form, so use form_tag instead: 但是您在这里不需要那个,您只需要一个搜索表单,因此请使用form_tag

<%= form_tag :action => 'search', :controller=>"ads" do %>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM