So i have a edit form which has year start and end date. I want to show db stored values as default dates.
I have tried this
<%= f.input :year_start_date, :label => 'Year Start Date', :as => :date_select, :include_blank => false,
:input_html => { :id => 'y_start_date'}, prompt: { day: year_start_date.day, month: Date::MONTHNAMES[year_start_date.month], year: year_start_date.year}, data: year_start_date %>
So its fine and started to show db stored values.
But the issue is when am changing only month, parameters will be
"year_start_date(1i)" => "", "year_start_date(2i)" => "3", "year_start_date(3i)" => ""
and in the controller am doing
[ school_params['year_start_date(1i)'], school_params['year_start_date(2i)'], school_params['year_start_date(3i)'] ].join('-').to_date
since it contains empty strings it throws invalid date error.
I want the default values( in this case date and year) when nothing is selected. How to achieve this?
Edit 1:
params
{
"utf8" => "✓",
"_method" => "patch",
"authenticity_token" => "abcffdgfdgfgfgf==",
"school" => {
"name" => "Test School",
"board_id" => "1",
"board_registration_number" => "",
"subdomain" => "testschool",
"email" => "",
"website" => "",
"address" => "NA",
"city" => "NA",
"pincode" => "",
"country_id" => "1",
"country_state" => "Tamil Nadu",
"phone" => "NA",
"inc_year" => "",
"handler_id" => "14451",
"year_start_date(1i)" => "",
"year_start_date(2i)" => "1",
"year_start_date(3i)" => "",
"year_end_date(1i)" => "",
"year_end_date(2i)" => "",
"year_end_date(3i)" => ""
},
"button" => "",
"controller" => "sat/schools",
"action" => "update",
"id" => "2"
}
Instead of using that ugly date/datetime input you can simply use the date picker provided by the browser without having to add additional libraries:
f.input :year_start_date, :label => 'Year Start Date', as: :string, input_html: { type: "date" }
Related
I try make feature when in chat there are some unacceptable message that any other users can reply on this message with command '/report' and admins received meesage from bot with inline keyboad where he can:
ban sender of report
ban reporter of message
So, for this I have two methods:
Action "/report" - it sends to admin inline keyboard(there is all
needed data)
"callback_query" - it processes message(don't have data what need
for ban of user) - it has only data of chat, user who clicked inline
button
Ok, i can save data from point 1 to redis and pass it into point 2, but how can I found(indenify) that variable to use it...
Any tips?
def report!(*)
Rails.cache.write(:update, update)
# create_report
# binding.pry
user_name_reporter = "#{update.dig('message', 'from', 'first_name')} #{update.dig('message', 'from', 'last_name')}"
user_name_sender = "#{update.dig('message', 'reply_to_message', 'from', 'first_name')} #{update.dig('message', 'reply_to_message', 'from', 'last_name')}"
respond_with :message, text: "We got report in the group!\n#{user_name_reporter} sent report on #{user_name_sender}", reply_markup: {
inline_keyboard: [
[
{text: t('.restrict_messages_for_sender'), callback_data: 'restrict_messages_for_sender'},
{text: t('.restrict_messages_for_reporter'), callback_data: 'restrict_messages_for_reporter'},
],
[
{text: t('.ban_for_sender'), callback_data: 'ban_for_sender'},
{text: t('.ban_for_reporter'), callback_data: 'ban_for_reporter'},
],
# [{text: t('.repo'), url: 'https://github.com/telegram-bot-rb/telegram-bot'}],
],
}
end
def callback_query(data)
report_update = Rails.cache.read(:update)
sender_user_id = report_update.dig('message', 'from', 'id')
reporter_user_id = report_update.dig('message', 'reply_to_message', 'from', 'id')
chat_id = report_update.dig('message', 'chat', 'id')
# binding.pry
case data
when 'restrict_messages_for_sender'
answer_callback_query t('.alert'), show_alert: true
bot.restrict_chat_member(chat_id: chat_id, user_id: sender_user_id, permissions: { can_send_messages: false }.to_json, until_date: (Time.now + 1.hours).to_i)
when 'restrict_messages_for_reporter'
answer_callback_query t('.no_alert')
bot.restrict_chat_member(chat_id: chat_id, user_id: reporter_user_id, permissions: { can_send_messages: false }.to_json, until_date: (Time.now + 1.hours).to_i)
when 'ban_for_sender'
answer_callback_query t('.no_alert')
bot.ban_chat_member(chat_id: chat_id, user_id: sender_user_id)
when 'ban_for_reporter'
answer_callback_query t('.no_alert')
bot.ban_chat_member(chat_id: chat_id, user_id: reporter_user_id)
end
end
data in action '/report' :
{
"update_id" => 156273636,
"message" => {
"message_id" => 523,
"from" => {
"id" => 308967324,
"is_bot" => false,
"first_name" => "PRD",
"last_name" => "RSD",
"username" => "anko20094",
"language_code" => "en"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272502,
"reply_to_message" => {
"message_id" => 522,
"from" => {
"id" => 1949490661,
"is_bot" => false,
"first_name" => "Den",
"username" => "ItshenotI",
"language_code" => "uk"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272484,
"text" => "unacceptable text, that must be reported"
},
"text" => "/report",
"entities" => [
[0] {
"offset" => 0,
"length" => 7,
"type" => "bot_command"
}
]
}
}
and here data in action 'callback_query':
{
"update_id" => 156273638,
"callback_query" => {
"id" => "1327004554721482603",
"from" => {
"id" => 308967324,
"is_bot" => false,
"first_name" => "PRD",
"last_name" => "RSD",
"username" => "anko20094",
"language_code" => "en"
},
"message" => {
"message_id" => 524,
"from" => {
"id" => 5362380113,
"is_bot" => true,
"first_name" => "anko_test",
"username" => "anko_test_bot"
},
"chat" => {
"id" => -686671946,
"title" => "test gropt for bots",
"type" => "group",
"all_members_are_administrators" => true
},
"date" => 1659272601,
"text" => "We got report in the group!\nPRD RSD sent report on Den",
"reply_markup" => {
"inline_keyboard" => [
[0] [
[0] {
"text" => "Restrict messages for sender user",
"callback_data" => "restrict_messages_for_sender"
},
[1] {
"text" => "Restrict messages for reported user",
"callback_data" => "restrict_messages_for_reporter"
}
],
[1] [
[0] {
"text" => "Ban for sender",
"callback_data" => "ban_for_sender"
},
[1] {
"text" => "Ban for reporter",
"callback_data" => "ban_for_reporter"
}
]
]
}
},
"chat_instance" => "-5057992267045364113",
"data" => "restrict_messages_for_sender"
}
}
and screenshot how it looks in telegram:in telegram
I'm having issues with a Ruby gem that used to work, but now doesn't. It's openstates https://rubygems.org/gems/openstates . The code to find Legislators doesn't seem to work anymore. We have had this code in production for a while, but are now getting this error
NoMethodError - undefined method `map' for #<String:0x007ff0b51941c0>
openstates (0.0.1) lib/openstates/model.rb:37:in `where'
This is the how we are calling the method.
OpenStates::Legislator.where(parameters)
parameters = {:state=>"Ca"}
Your code hasn't changed, the gem hasn't changed, but the API probably has.
Here's a very basic module to help you get an Array from OpenStates::Legislator.where(parameters) :
require 'open-uri'
require 'active_support/core_ext/object'
module OpenStates
class Legislator
#cache={}
class << self
def where(parameters)
url = "https://openstates.org/api/v1/legislators/?#{parameters.to_query}"
#cache[url] ||= get_json(url)
end
private
def get_json(url)
puts "# Downloading #{url}"
sleep 2
JSON.load(open(url))
end
end
end
end
parameters = {:state=>"Ca"}
p OpenStates::Legislator.where(parameters).first(2)
It outputs :
# Downloading https://openstates.org/api/v1/legislators/?state=Ca
[
{
"last_name" => "Gatto",
"updated_at" => "2016-09-24 07:16:00",
"nimsp_candidate_id" => nil,
"full_name" => "Mike Gatto",
"id" => "CAL000123",
"first_name" => "Mike",
"middle_name" => "",
"district" => "43",
"chamber" => "lower",
"state" => "ca",
"votesmart_id" => "120220",
"party" => "Democratic",
"+capitol_office" => {
"phone" => "(916) 319-2043",
"street" => "P.O. Box 942849, Room 4140",
"zip" => "94249-0043",
"city" => "Sacramento"
},
"all_ids" => [
"CAL000123",
"CAL000246",
"CAL000246",
"CAL000367"
],
"leg_id" => "CAL000123",
"active" => true,
"transparencydata_id" => "76584a5322274b9b892642b7b6ae3db5",
"photo_url" => "http://assembly.ca.gov/sites/assembly.ca.gov/files/memberphotos/AD43.jpg",
"+district_offices" => [
{
"phone" => "(818) 240-6330",
"street" => "300 East Magnolia, Suite 504",
"zip" => "91502",
"city" => "Burbank"
}
],
"url" => "http://asmdc.org/members/a43",
"country" => "us",
"created_at" => "2012-01-31 19:25:09",
"level" => "state",
"+district" => "43",
"offices" => [
{
"fax" => nil,
"name" => "Capitol Office",
"phone" => "916-319-2043",
"address" => "P.O. Box 942849, Room 5136\nSacramento, CA 94249-0043",
"type" => "capitol",
"email" => nil
},
{
"fax" => nil,
"name" => "District Office",
"phone" => "818-558-3043",
"address" => "300 East Magnolia Blvd, Suite 504\nBurbank, CA 91502",
"type" => "district",
"email" => nil
}
],
"+party" => "Democratic",
"suffixes" => ""
},
{
"last_name" => "Gordon",
"updated_at" => "2016-09-24 07:16:01",
...
When things went wrong, Rails echoes a dump of given params, it looks like this:
Parameters:
{"utf8"=>"✓",
"filter"=>{"title"=>"1",
"address"=>"2",
"city"=>{"title"=>"3"},
"people"=>{"name"=>"4",
"surname"=>"5",
"patronymic"=>"6",
"emails"=>{"email"=>"7"}},
"emails"=>{"email"=>"8"}},
"sort"=>{"0"=>{"field"=>"",
"dir"=>"asc"},
"1"=>{"field"=>"",
"dir"=>"asc"}},
"commit"=>"Show"}
But it actually would be much more useful if looks like this:
{
"utf8" => "✓",
"filter" => {
"title" => "1",
"address" => "2",
"city" => {
"title" => "3"
},
"people" => {
"name" => "4",
"surname" => "5",
"patronymic" => "6",
"emails" => {
"email" => "7"
}
},
"emails" => {
"email" => "8"
}
},
"sort" => {
"0" => {
"field" => "",
"dir" => "asc"
},
"1" => {
"field" => "",
"dir" => "asc"
}
},
"commit" => "Show"
}
How to achieve that?
With vanilla Ruby the nicest way is to turn the hash to JSON:
JSON::pretty_generate(hash_here, :allow_nan => true, :max_nesting => false)
This outputs a prettified JSON version of your object (array, hash or whatever). It won´t be pure ruby but this comes right at the base library so there is no need to install anything.
I have been trying to set my container class and id options for a directions maps without any success... anyone knows how to?
From documentation:
<%= gmaps( :map_options => { :container_class => "foo", :id => "bar", :class => "baz" } %>
<%= gmaps({"direction" => { "data" => { "from" => "Paris, france", "to" => "Toulon, france" } }})
%>
I tried combining both but it just won't work...
Cheers,
Joel
You only want a single map right?
In this case:
<%= gmaps( :map_options => { :container_class => "foo", :id => "bar", :class => "baz" },
"direction" => { "data" => { "from" => "Paris, france", "to" => "Toulon, france" }}
) %>
I'm sure there has to be a very simple solution to my problem but i don't know why i cant seem to put my finger on it, the problem is that I'm trying to submit a form but every time i submit the form the create action controller gives the following error
Mysql2::Error: Column 'preffered_players' cannot be null: INSERT INTO saved_sessions (preffered_players, session_id, preffered_opponents, game_id, game_type, opponents_list, banned_players, players_list) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
how ever all these values that the error says are null are passed through parameters as soon as the form is submitted, can anyone tell me how to use the parameter values that i get from the form to fill in the little nulls in my query, create controller is as follow
def create
opponents_list = params[:opponents_list]
banned_players = params[:banned_players]
game_type = params[:game_type]
session_id = params[:session_id]
preffered_opponents = params[:preffered_opponents]
game_id = params[:game_id]
players_list = params[:players_list]
preffered_players = params[:preffered_players]
#saved_sessions = SavedSession.new(params[:saved_sessions])
respond_to do |format|
if #saved_sessions.save
format.html { redirect_to(#game, :notice => 'Game was successfully created.') }
format.xml { render :xml => #game, :status => :created, :location => #game }
else
format.html { render :action => "new" }
format.xml { render :xml => #game.errors, :status => :unprocessable_entity }
end
end
end
thanks for any advice...
i'm including the form code however its very messy(its a huge form) plus its in haml :)
- form_tag('/saved_sessions',:method => :post, :id => "new_game") do
.select_option
.list_col1
.fl
%img{:alt => "image", :border => "0", :src => "/images/happy_face.png"}/
.blue_col
= text_field_tag :preffered_opponents, "", :class => :blue_bar
.clr
.list_col2
.list_col2_top_row
.radio_row
.fl
%input{:type => "radio", :name => "battle_scale", :value => "3"}
.fl 3 vs 3
.fl
%input{:type => "radio", :name => "battle_scale", :value => "5"}
.fl 5 vs 5
.clr
%div
.fl
%img{:alt => "image", :border => "0", :src => "/images/sad_face.png"}/
.fl
/%input#banned_players.white_bar{:type => "text"}/
= text_field_tag :banned_players, "", :class => :white_bar
.clr
.versus_row
#inputs-vs.versus_col1
.versus_col_row
.blue_col
/%input#input-1-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-1-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-2-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-2-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-3-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-3-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-4-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-4-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col_row
.blue_col
/%input#input-5-vs.blue_bar{:type => "text"}/
= text_field_tag 'input-5-vs', "", :class => :blue_bar
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.clr
.versus_col2
%img{:alt => "0", :border => "0", :src => "/images/vs.png"}/
#inputs.versus_col1
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-1.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-1', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-2.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-2', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-3.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-3', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-4.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-4', "", :class => :pink_bar
.clr
.versus_col_row
.fl
%img{:alt => "image", :border => "0", :src => "/images/lock.png"}/
.blue_col
/%input#input-5.pink_bar{:name => "blah", :type => "text"}/
= text_field_tag 'input-5', "", :class => :pink_bar
.clr
.clr
.session_row
%p
%b You can save and share this session by entering your email. A unique code will be sent
%p.small_text (Session are stored for 30 days)
.seesion_send_row
.fl
/%input.big_white_bar{:type => "text"}/
= text_field_tag 'email_bar', "", :class => :big_white_bar
.fl
%span 2+2=
%span
%span
/%input.small_bar{:type => "text"}/
= text_field_tag 'code_bar', "", :class => :small_bar
.fl
/%a{:href => "#."}
/%img{:alt => "image", :border => "0", :src => "/images/send_button.png"}/
= submit_tag("Send")
hope this can help you guys better understand the problem..
You seem to be pulling from 2 different places. The block of code at the top of the method is pulling the values from the top level of the incoming params, but then later you initialize a new SavedSession using the params[:saved_sessions] input. The two things don't seem to match...
if saved_sessions.opponents_list is found at opponents_list = params[:opponents_list] in the first line, then when you later try to save params[:saved_sessions], you're getting it from a different spot in the params...
EDIT: so you do not need both of the sets of values you have in the action. if you build your form like this (or at least something like this) you'll get the params correctly and can just blindly send them into SavedSession.new, and then call .save.
- form_for(#aved_session) do |f|
-.blue_col
= f.text_field :preffered_opponents, "", :class => :blue_bar
If you allow f.text_field to build the id and name then rails magic will make sure that they are named/id'd correctly and then more rails magic interprets them and builds your input parameters correctly. This will build your params as:
params = {:saved_session => {:preferred_opponents => "text value" }...
Then you can pull those params out:
saved_session_input = params[:saved_session]
and pass them into new just like you are. the difference is that now those parameters are actually IN there.
The way you have it is like this:
params = {:preferred_opponents => "text value", :saved_session => {:preferred_opponents => nil }...
this means you're passing nil in for the expected values for new. Note that you don't actually assign your local values to the new saved_session before calling save.
I hope this is clearer.