The action 'Search' cannot be found for InboxController - ruby

I am trying to add a search bar. I have also set the path. But everytime I try to click on search it directs me to this error. What is the error in this code?
This is my Inbox_Controller file. It says that the action 'Search' cannot be found in 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
This is my relevant index.erb.html file.
<%= form_tag inbox_search_path, :method => 'get' do %>
<p>
<%= search_field_tag :Search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
This is my relevant routes.rb file:
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'

There is no search method in this controller, the only search I see is a call to Inbox.search.
To debug this, start with the view where you actually do the "click". Is that click really supposed to trigger an action in your InboxController? If you think it should, why is there no action in that controller? If not, then the "click" was meant to go to another controller that actually would handle the search action, in which case you need to figure out why the "click" is trying to call a method in your InboxController rather than the desired controller. The problem could be something in your view or something in you routes, or you really should have that method in you InboxController, either way I suggest you try to figure out at least what should be happening and then post some more code stating what you think should be happening vs what is really happening.

Related

How to test WebSockets For Hanami?

Using the following:
Hanami cookbook websockets
IoT Saga - Part 3 - Websockets! Connecting LiteCable to Hanami
I've been able to add WebSockets to Hanami, however as this is for production code I want to add specs; but I can't find information on how to test WebSockets and Hanami using Rspec.
I've been able to find this for RoR but nothing non-Rails specific or Hanami Specific, I have asked on the Hanami Gitter but not gotten a response yet.
Is the TCR gem the only way? I would prefer something simpler but If I must how would I set it up for anycable-go via litecable.
How can I test WebSockets for Hanami using Rspec?
To get this working requires several moving parts, the first is the Socket simulator which simulates the receiving socket on the webserver:
Note: url_path should be customized to what works for your web socket specific endpoint
# frozen_string_literal: true
require 'puma'
require 'lite_cable/server'
require_relative 'sync_client'
class SocketSimulator
def initialize(x_site_id_header: nil)
#server_logs = []
#x_site_id_header = x_site_id_header
end
attr_accessor :server_logs
def client
return #client if #client
url_path = "/ws?connection_token=#{connection_token}"
#client = SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: headers, cookies: '')
end
def connection_token
#connection_token ||= SecureRandom.hex
end
def user
return #user if #user
email = "#{SecureRandom.hex}#mailinator.com"
password = SecureRandom.hex
#user = Fabricate.create :user, email: email, site_id: site_id, password: password
end
def start
#server = Puma::Server.new(
LiteCable::Server::Middleware.new(nil, connection_class: Api::Sockets::Connection),
Puma::Events.strings
).tap do |server|
server.add_tcp_listener '127.0.0.1', 3099
server.min_threads = 1
server.max_threads = 4
end
#server_thread = Thread.new { #server.run.join }
end
def teardown
#server&.stop(true)
#server_thread&.join
#server_logs.clear
end
def headers
{
'AUTHORIZATION' => "Bearer #{jwt}",
'X_HANAMI_DIRECT_BOOKINGS_SITE_ID' => #x_site_id_header || site_id
}
end
def site_id
#site_id ||= SecureRandom.hex
end
def jwt
#jwt ||= Interactors::Users::GenerateJwt.new(user, site_id).call.jwt
end
end
The next thing is the SyncClient which is a fake client you can use to actually connect to the simulated socket:
# frozen_string_literal: true
# Synchronous websocket client
# Copied and modified from https://github.com/palkan/litecable/blob/master/spec/support/sync_client.rb
class SyncClient
require 'websocket-client-simple'
require 'concurrent'
require 'socket'
WAIT_WHEN_EXPECTING_EVENT = 5
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
attr_reader :pings
def initialize(url, headers: {}, cookies: '')
#messages = Queue.new
#closed = Concurrent::Event.new
#has_messages = Concurrent::Semaphore.new(0)
#pings = Concurrent::AtomicFixnum.new(0)
#open = Concurrent::Promise.new
#ws = set_up_web_socket(url, headers.merge('COOKIE' => cookies))
#open.wait!(WAIT_WHEN_EXPECTING_EVENT)
end
def ip
Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
end
def set_up_web_socket(url, headers)
WebSocket::Client::Simple.connect(
url,
headers: headers
) do |ws|
ws.on(:error, &method(:on_error))
ws.on(:open, &method(:on_open))
ws.on(:message, &method(:on_message))
ws.on(:close, &method(:on_close))
end
end
def on_error(event)
event = RuntimeError.new(event.message) unless event.is_a?(Exception)
if #open.pending?
#open.fail(event)
else
#messages << event
#has_messages.release
end
end
def on_open(_event = nil)
#open.set(true)
end
def on_message(event)
if event.type == :close
#closed.set
else
message = JSON.parse(event.data)
if message['type'] == 'ping'
#pings.increment
else
#messages << message
#has_messages.release
end
end
end
def on_close(_event = nil)
#closed.set
end
def read_message
#has_messages.try_acquire(1, WAIT_WHEN_EXPECTING_EVENT)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
msg
end
def read_messages(expected_size = 0)
list = []
loop do
list_is_smaller = list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT
break unless #has_messages.try_acquire(1, list_is_smaller)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
list << msg
end
list
end
def send_message(message)
#ws.send(JSON.generate(message))
end
def close
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
raise "#{#messages.size} messages unprocessed" unless #messages.empty?
#ws.close
wait_for_close
end
def wait_for_close
#closed.wait(WAIT_WHEN_EXPECTING_EVENT)
end
def closed?
#closed.set?
end
end
The last part is a fake channel to test against:
# frozen_string_literal: true
class FakeChannel < Api::Sockets::ApplicationChannel
identifier :fake
def subscribed
logger.info "Can Reject? #{can_reject?}"
reject if can_reject?
logger.debug "Streaming from #{stream_location}"
stream_from stream_location
end
def unsubscribed
transmit message: 'Goodbye channel!'
end
def can_reject?
logger.info "PARAMS: #{params}"
params.fetch('value_to_check', 0) > 5
end
def foo
transmit('bar')
end
end
To use in specs:
# frozen_string_literal: true
require_relative '../../../websockets-test-utils/fake_channel'
require_relative '../../../websockets-test-utils/socket_simulator'
RSpec.describe Interactors::Channels::Broadcast, db_truncation: true do
subject(:interactor) { described_class.new(token: connection_token, loc: 'fake', message: message) }
let(:identifier) { { channel: 'fake' }.to_json }
let(:socket_simulator) { SocketSimulator.new }
let(:client) { socket_simulator.client }
let(:user) { socket_simulator.user }
let(:connection_token) { socket_simulator.connection_token }
let(:channel) { 'fake' }
let(:message) { 'woooooo' }
before do
socket_simulator.start
end
after do
socket_simulator.teardown
end
describe 'call' do
before do
client.send_message command: 'subscribe',
identifier: identifier
end
it 'broadcasts a message to the correct channel' do
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
end
context 'with other connection' do
let(:user2) { Fabricate.create :user }
let(:jwt) { Interactors::Users::GenerateJwt.new(user2, site_id).call.jwt }
let(:site_id) { socket_simulator.site_id }
let(:url_path) { "/ws?connection_token=#{SecureRandom.hex}" }
let(:client2) { SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: {}, cookies: '') }
before do
client2.send_message command: 'subscribe',
identifier: identifier
end
it "doesn't broadcast to connections that shouldn't get it" do
aggregate_failures 'broadcast!' do
expect(client2.read_message).to eq('type' => 'welcome')
expect(client2.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
sleep 1
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
expect { client2.close }.not_to raise_exception
end
end
end
end
end

Rails - Render Ajax inside a loop

I'd like to update several single table cell in my view with ajax.
Is there a chance to run the uptime partial several times during the loop?
Currently the loop iterates over all given records but the partial runs once.
def CheckUptimes
require 'net/ssh'
#username = "updater"
#password = "üqwlp+ß$2"
#cmd = "uptime"
#items = Item.all.where("(category = 'Ubuntu 14 LTS')")
#items.each do |ci|
#hostname = ci.name
begin
ssh = Net::SSH.start(#hostname, #username, :password => #password)
#uptime = ssh.exec!(#cmd)
ssh.close
#uptime = #uptime.strip
ci.update_attributes(:uptime => #uptime)
respond_to do |format|
format.html
format.js { render :partial => "uptime", :locals => { :id => ci.id, :uptime => #uptime } }
end
rescue
puts "Unable to connect to #{#hostname} using #{#username}/#{#password}"
end
end
end
If I understand well, I think you could save in two instance variables (Arrays) which hostnames were able to to connect and which not, and then in checkuptimes.js.erb you can show which ones are ok with render collection
Something like this
#con_ok=#con_ko=[]
#items.each do |ci|
#hostname = ci.name
begin
ssh = Net::SSH.start(#hostname, #username, :password => #password)
#uptime = ssh.exec!(#cmd)
ssh.close
#uptime = #uptime.strip
ci.update_attributes(:uptime => #uptime)
con_ok<<ci.id
rescue
con_ko<< ci.id
end
respond_to do |format| ## Not necessary ##
format.html
format.js
end
in checkuptimes.js.erb
$("#mydiv").html("<%= escape_javascript(render 'uptime', collection: #con_ok)%>");
in this example, the partial uptime will be rendered as many times as items contains #con_ok, with a local variable con_ok with the the item in the array (id)

how to display json and parse json in ruby using httparty

I am working on a food app in ruby on rails which requires to get calorie value of food item from food api.In my controller i am getting the JSON response but i am unable to parse and display the calorie value of food item in a index.html.erb file here is my controller code.
require 'rubygems'
require 'httparty'
class FoodsController < ApplicationController
def index
#foods = Food.all
end
def show
#food = Food.find(params[:id])
end
def new
#food = Food.new
end
def edit
#food = Food.find(params[:id])
end
def create
#food = Food.new(food_params)
#response = HTTParty.get('http://api.nutritionix.com/v1_1/search/'+#food.name+'?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_serving_size_unit%2Cnf_calories%2Cnf_total_fat&appId=696d1ad4&appKey=aec2c4766d40d7f6346ed89d5d82fe75')
#http_party_json = JSON.parse(#response.body)
if #food.save
redirect_to foods_path
else
render 'new'
end
end
def update
#food = Food.find(params[:id])
if #food.update(food_params)
redirect_to #food
else
render 'edit'
end
end
def destroy
#food = Food.find(params[:id])
#food.destroy
redirect_to foods_path
end
private
def food_params
params.require(:food).permit(:name, :quantity)
end
end
Any suggestions are highly welcome as i am newbie on stackoverflow so dont know proper editing forgive please! help me how to display calorie value in html page
You can add a new function to Food model to get you the Calorie:
class Food
def calorie
response = HTTParty.get("http://api.nutritionix.com/v1_1/search/#{self.name}?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_serving_size_unit%2Cnf_calories%2Cnf_total_fat&appId=696d1ad4&appKey=aec2c4766d40d7f6346ed89d5d82fe75")
json = JSON.parse(response.body)
end
end
and then simply in your index.erb if you loop over foods collection you do the following:
<% #foods.each do |food| %>
<%= food.name %>
<%= food.calorie %>
<% end %>
but in that case performance will not be good, as you do remote access for each item each time you display data, so as calorie value is always the same for same food, then after its created you can do remote query and store the calorie to calorie attribute in your Food model
You can do the following:
class Food < ActiveRecord::Base
before_create :set_calorie
private
def set_calorie
response = HTTParty.get("http://api.nutritionix.com/v1_1/search/#{self.name}?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_serving_size_unit%2Cnf_calories%2Cnf_total_fat&appId=696d1ad4&appKey=aec2c4766d40d7f6346ed89d5d82fe75")
self.calorie = JSON.parse(response.body)
end
end

Strong params, unpermitted parameters

I'm having an issue with strong parameters.
My permuted parameters are:
def post_params
params.require(:post).permit(:content, :user_id, :topic_id).merge(:user_id => get_user.id)
end
The parameters being passed are:
{"utf8"=>"✓",
"authenticity_token"=>"5+OEnLgihamJC37BSn4r/spoiRmccJzHhe6eaeC2Fuc=",
"post"=>{"topid_id"=>"10",
"content"=>"awfawfaw"}}
And the create function is:
def create
post = Post.new(post_params)
if post.valid? && post.save
redirect_to :controler => :topic, :action => :show, :topic => post.topic.id
end
end
That is the error in the console. I want to know why it is not permitting topic_id.
You have a typo in your permitted params:
what you have is:
def post_params
params.require(:post).permit(:content, :user_id, :topic_id).merge(:user_id => get_user.id)
end
and It should be:
def post_params
params.require(:post).permit(:content, :user_id, :topid_id).merge(:user_id => get_user.id)
end
But it depends on your model what name of attribute you have in your model. Either you have to change in the form in typo in the form, or either you have to change everywhere else.

rails sidekiq background process

i'm having an issue configuring the sidekiq server, the process seems to be running in the foreground as soon as i refresh my page. /consumers/fetch i need to put it in the background permanently.
consumers_controller.rb
require 'kafka'
class ConsumersController < ApplicationController
def fetch
#consumer = Kafka::Consumer.new( { :host => ENV["host"],
:port => ENV["port"],
:topic => ENV["topic"]})
#consumer.loop do |message|
logger.info "-------------#{message.inspect}--------------"
logger.info "-------------#{message.first.payload.inspect}--------------"
unless message.blank?
ConsumerWorker.perform_async(message.first.payload)
end
end
end
end
consumer_worker.rb
class ConsumerWorker
include Sidekiq::Worker
def perform(message)
payload = message.first["payload"]
hash = JSON.parse(payload)
return #message = Message.new(hash) if hash["concern"] == 'order_create' or hash["concern"] == 'first_payment'
end
end
message.rb
class Message
attr_reader :bundle_id, :order_id, :order_number, :event
def initialize(message)
#payload = message["payload"]
#bundle_id = #payload["bundle_id"]
#order_id = #payload["order_id"]
#order_number = #payload["order_number"]
#event = message["concern"]
end
end
I think you need to move this block
#consumer.loop do |message|
end
inside your worker somehow, as I think the consumption is done after block execution.

Resources