无法找到InboxController的“搜索”操作

时间:2015-05-21 23:42:30

标签: ruby ruby-on-rails-3

我正在尝试添加搜索栏。我也设定了路径。但每次我尝试点击搜索它都会指示我这个错误。这段代码中的错误是什么?

这是我的Inbox_Controller文件。它说在InboxController中找不到“搜索”动作。

 class InboxController < ApplicationController
 before_action :valid_membership
 before_action :change_password_next_login
 before_action :agreed_to_terms
 before_action :allowed_send_mail?

 layout 'inbox'

 def bulk
 puts params

 ids = params[:bulk_ids]

 if ids
  params[:commit]

  case params[:commit]
    when 'Archive'
      ids.each do |id|
        message = Message.find(id)
        message.archived = true
        message.save()
      end
    when 'Restore'
      ids.each do |id|
        message = Message.find(id)
        message.archived = false
        message.save()
      end
    else
      puts 'invalid action!!'
  end


  if params[:folder] != ''
    redirect_to inbox_index_path(folder: params[:folder])
  else
    redirect_to inbox_index_path
  end
 else
  flash[:alert] = t('errors.inbox.no_emails_selected')
  redirect_to :back
 end
 end

def index

per_page = 10
page = params[:page] ? params[:page] : 1
@inbox = Inbox.search(params[:search])
  case params[:folder]
  when 'archive'
    @messages = current_user.archived_messages(page, per_page)
  when 'drafts'
    @messages = current_user.draft_messages(page, per_page)
  when 'sent'
    @messages = current_user.sent_messages(page, per_page)
  else
    @messages = current_user.received_messages(page, per_page)
  end

  end

  def reply
  original = Message.find(params[:id])
  @quoted = "\n\nOn #{original.sent_time.strftime("%m/%d/%y %-I:%M %p")}, # {original.from.full_name} wrote:\n----------------\n#{original.body}"
@message = Message.new(
    :parent => original,
    :to => original.from,
    :subject => "RE: #{original.subject}",
    :body => @quoted,
)

render :compose
end

def move
@message = Message.find(params[:id])
folder = params[:destination]

case folder
  when 'archive'
    @message.archived = true
  else
    @message.archived = false
end

unless @message.save
  puts @message.errors.full_messages
end

redirect_to inbox_index_path(folder: folder)
end

def show
@message = Message.find(params[:id])

if !@message.read? && @message.to == current_user
  @message.read_time = DateTime.now
  unless @message.save
    puts @message.errors.full_messages
  end
end
end

def edit
@message = Message.find(params[:id])
@message.to_name = @message.to.full_name
render 'compose'
end

  def compose
   @message = Message.new
   if(params[:id])
     @message.to = Mentor.find(params[:id])
   end

  end

  def create
    if(params[:message] && !params[:message][:id].empty?)
      @message = Message.find(params[:message][:id])
      @message.assign_attributes(message_params)
    else
      @message = Message.new(message_params)
    end

     if params[:parent_id] && !params[:parent_id].empty?
     @message.parent = Message.find(params[:parent_id])
     @message.replied_to_time = Time.now
    end
    @message.from = current_user

    draft = params[:draft]

    if draft
      @message.draft = true
    else
      @message.sent_time = Time.now
      @message.draft = false
    end

     # puts @message.as_json
    if can_send_mail
      if @message.save
        if !draft
          if current_user_or_guest.mentee?
            current_user.credits += -1
            current_user.save
          end

         UserMailer.inmail_notification(@message).deliver
        end

        redirect_to inbox_index_path(folder: draft ? 'drafts' : 'inbox'), notice: "Message successfully #{draft ? 'saved' : 'sent'}."
      else
        flash.now[:alert] = 'All email fields need to be filled out prior to sending/saving.'
        render 'compose'
      end
    else
      flash.now[:alert] = 'You do not have enough credits to send any more    InMail to Game Changers.'
      render 'compose'
    end
   ActivityLog.create(userid: current_user.id, points: 500, typeof: "message")
  end 

  def allowed_send_mail?
    unless !current_user.admin?
      msg = "You are not authorized to access this page!"
     show_error(msg)
   end
  end

  def profile_complete
    return true if current_user.mentor?

    unless current_user.profile_complete?
      flash[:alert] = t('errors.inbox.incomplete_profile')
      redirect_to edit_user_registration_path
    end
  end

  def message_params
    params.require(:message).permit(:id, :to_name, :to_id, :subject, :body)
  end
end

这是我的相关index.erb.html文件。

<%= form_tag inbox_search_path, :method => 'get' do %>
    <p>
      <%= search_field_tag :Search, params[:search] %>
      <%= submit_tag "Search", :name => nil %>
    </p>
<% end %>

这是我的相关routes.rb文件:

 get 'inbox' => 'inbox#index', :as => 'inbox_index'
 get 'inbox/show/:id' => 'inbox#show', :as => 'inbox_show'
 get 'inbox/compose' => 'inbox#compose', :as => 'inbox_compose'
 get 'inbox/compose/:id' => 'inbox#compose', :as => 'inbox_compose_to'
 get 'inbox/edit/:id' => 'inbox#edit', :as => 'inbox_edit'
 get 'inbox/move' => 'inbox#move', :as => 'inbox_move'
 get 'inbox/reply' => 'inbox#reply', :as => 'inbox_reply'
 get 'inbox/search' => 'inbox#search', :as => 'inbox_search'
 post 'inbox/create' => 'inbox#create'
 post 'inbox/bulk' => 'inbox#bulk'

1 个答案:

答案 0 :(得分:0)

此控制器中没有搜索方法,我看到的唯一搜索是对Inbox.search的调用。

要对此进行调试,请从实际执行&#34;单击&#34;的视图开始。点击是否真的应该在你的InboxController中触发一个动作?如果您认为应该,那为什么控制器中没有动作?如果没有,那么&#34;点击&#34;本来打算去另一个实际上会处理搜索操作的控制器,在这种情况下你需要找出为什么&#34;点击&#34;试图在你的InboxController中调用一个方法而不是所需的控制器。问题可能在你的视图中或你的路线中的某些东西,或者你真的应该在你的InboxController中有这种方法,无论哪种方式我建议你试图弄清楚应该发生什么,然后发布更多的代码说明你的想法应该发生与真正发生的事情。