Can't access rails post data - html

I've spent the last couple of hours slowly losing my mind while trying to figure something out. I'm submitting a form in rails, everything works fine until I try to access the params. In the console I can see the following:
Parameters: {"authenticity_token"=>"3mdEW2lHhkzpZbDsJCu8ZEV/wbq2YB/ztNR0RLTMZDs=", "utf8"=>"✓", "project"=>{"name"=>"woeij", "client"=>"iwej", "description"=>"oiejdoiew woeij"}, "id"=>"13"}
As you can see I'm sending name, client, description and id. I can access ID fine with something like:
#id = params[:id]
However, when I try to access name, client, or description in the same way they're all empty.
If I do:
#project = params[:project]
I get:
namewoeijclientiwejdescriptionoiejdoiew woeij
Would someone mind explaining what I'm doing wrong? And why I can't just get "woeij" when I do:
#name = params[:name]
Sorry for the stupid question, big thanks as always.

Attributes are nested, do
params[:project][:name]
to retrieve name.
A really cool tool in the rails console is the y: if you type y params they'll be presented really nicely.

You have a hash inside a hash. After you do:
#project = params[:project]
You have all your project parameters inside that hash. You can select them like this:
#project[:name] #=> "woeij"
#project[:client] #=> "iwej"
You can also select them in one go like this:
params[:project][:description] #=> "oiejdoiew woeij"

Related

Reaction Roles using JSON files

So.. I want to know how could I do the classic events work with my json variables.
Let me explain, I created a json file which memorates the, channelid, reactions, assigned role, and total reactions, of a message. For example if I run the command :
.r <messageID> <emoji> <role> add
The JSON File writes this :
{
<messageID> :{
'channelID' : <channelID of the message>
'reaction(1)' : '<emoji>' //unicode name value | I used "unicode.name(emoji)"
'assigned role(1)' : '<role>'
'total rrs' : 1
}
}
I ve made everything, but the events, I can t figure them out, I tried with payload, that on_reaction_raw_add/remove functions, but it does not work, or supposely I don t know how to use them..
I also tried with the other ones, on_reaction_add/remove() but I can't figure them out, it does not work, could omeone help me, in case you need more details about the code, you can comment below and I ll edit the message or reply yours.
Thank you,
RVZWN.
I assume the events in general, there is an event called 'on_reaction_add' which lets you do something whenever you add a reaction ( 'on_reaction_remove' for when you remove one )
Inside this method, you can get the reaction from the user who placed it.
in the reaction value you can get the actual message by doing reaction.message ( Discord.Message ) and reaction.emoji ( which gives you the actual emoji )
async def on_reaction_add(self, reaction: discord.Reaction, user:
discord.Member):
# Rest of code here about reactions, dumping to json and things
If I didn't answer it correctly then specify what you're asking,
If you need anything else I'll be more than happy to help.

Not like on Array field

here is the problem I'm stuck with:
I'm using Rails 4 & MySQL
I've Message which have one sender and one recipient.
I want to be able to archive messages but if sender archive a message, the recipient still can access to the message until he archive it too.
I've serialize a field :
serialize :archived_by, Array
which contains which user archived the message
but I can't figure out how to query with it.
Message.where("archived_by like ?", [1].to_yaml)
works well, returning messages archived by User '1'
Message.where.not("archived_by like ?", [1].to_yaml)
won't work, returning nothing
I would like to find something else than using a classic many to many ...
Thanks!
UPDATE
I finally decided to add 2 fields, one for the sender & one for the recipient to know which archived the message. If someone has the proper way to do this, tell us :)
If you are using postgresql you could query the informations.
As in answer Searching serialized data, using active record described, the downsize of serializer at least under mysql is, that you byepass native db abstraction.

Finding a specific value out of an Array (RoR / MySQL)

I am trying to find a specific value inside an array. The array is composed from data in MySQL database and looks like:
info = [#<Info1: bla, Info2: blo>,#<Info1: bli, Info2, Ble>]
Now I want to get every Info1's value from it, but I do not know how.
The array was formed by calling
info = Info.find(:all)
Can anyone help me?
I am using Rails 2.2.2 (don't ask, can't do anything about it) and Ruby 1.8.
Edit: More details
Info is a database, where Info1 and info 2 are the columns. Calling it with info = Info.find(:all) returns the array above.
What I have tried so far involves trying to go through the array with each, but so far no luck.
Most of what I have tried like
a.grep(/^info1/)
and
info.select(|i| i.name == "info1")
all return empty arrays
Edit
Nevermind, I found the answer. I was thinking too weird. The answer is
info.each do |object|
puts object.info2
end
What's your selection criteria? You can do something like
info.select{|i| i.name == 'hello' }
and you will get all the Info objects with name = 'hello'.
But I would prefer to change the query, if you can, to filter them in the database query directly.

Rails Database Query

I am trying to gather information from a database and then pass that information on to my view via flash. Unfortunately the information is formatting in an ActiveRecord::Relation and not in any format that I can read.
Controller Query
#message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)
flash[:response] = #message
redirect_to (:back)
If I do something like
#message = Message.where(:all).limit(4).order(id: :desc).only(:order,:where)
flash[:response] = #message.first.mess
redirect_to (:back)
To try and get the first returned value in the mess column, I get an undefined method error. I have been trying to find a tutorial that tells me how to take information once my query has been run but I have not had much luck with Rail's tutorials as of late. I appreciate any help that you guys can give. After I do this I am going to try to format the 4 different results on the view side.
List of messages:
#messages = Message.order(id: :desc).limit(4)
This: only(:order,:where) cancels your limit(4) (why?)
#messages is now an activerecord association, not suitable to output.... So, if you have 4 messages top you can do:
if #messages.any? # maybe no messages come out
flash[:response] = #messages.map(&:mess).join('<br>') # add an html newline between them
end
redirect_to :back

Pass in user-specified parameters to query a database and return data

I am extremely new to Ruby on Rails, having only a couple days of experience. So far, I have created a new app, and loaded data into the database called name which is comprised of date:string, value:decimal, and unique_id:integer. So now, I can go to "(my local port)/name" and view the table successfully.
What I would like to do is this:
In a new html page, have a SIMILE Timeplot (http://www.simile-widgets.org/timeplot/) with an HTML drop-down list below it in order to select a unique_id and another drop-down box to select a year.
From there, I would like to search through the database and display all of the data on the Timeplot that matches the unique_id and that is in the specified year.
I believe I must make an HTTP GET request for a date_to, date_from, and unique_id, but I do not know how to implement this (admittedly I have been searching the web for ages, but could not figure out the solution).
Any help would be greatly appreciated! Thank you.
Edit: Even just advice on what component to tackle first
First you need to create a route for your search such as this:
match "name/search" to: "name#search" as: "name_search", via: :get
Then if you are using AJAX, and using jQuery you make an HTTP request like this:
$.get("/name/search", {
unique_id: <your_unique_id>,
date_from: <your_date_from>,
date_to: <your_date_to> },
function(result) {
// You do whatever you want with the result here
}
}
P.S:
The Javascript code might not be 100% correct, since I rarely use it.