Locally it records a string as expected and on Heroku it records a fixnum...
A user can create a wish and select an area where he wants to see a concert. In order to receive an email if a concert matches his wish.
I have a hash sotred in my application_helper.rb as a constant
DEPARTMENTS = {
"01" => "Ain",
"02" => "Aisne",
"03" => "Allier"
#........
}
My create method in the wanted_concerts_controller.rb looks like this
def create
#wanted_concert = WantedConcert.new(wanted_concerts_params)
#wanted_concert.user_id = current_user.id
if #wanted_concert.save!
redirect_to wanted_concerts_path, notice: "Ton souhait est bien enregistré"
else
render :new , alert: "Oups recommence!"
end
end
private
def wanted_concerts_params
params.require(:wanted_concert).permit(:department, :user_id)
end
In the new.html.erb file I can make my wish
<%= simple_form_for(#wanted_concert) do |f| %>
<%= f.input :department, prompt: "Choisi une région", label: false, collection: ApplicationHelper::DEPARTMENTS.map { |k, v| v } %>
<%= f.submit "Valider", class: "btn btn-success" %>
<% end %>
And this is my index.html.erb where the wishes are displayed
<% #wanted_concerts.each do |wanted| %>
<li> <%= wanted.department %> <%= link_to "Supprimer", wanted_concert_path(wanted), method: :delete %></li>
<% end %>
So locally if I chose for exemple Ain
the index display Ain
and on Heroku if chose Ain
it display 0
So On Heroku console I did:
irb(main):004:0> c = WantedConcert.last
D, [2018-03-04T13:31:54.314672 #4] DEBUG -- : WantedConcert Load (1.3ms) SELECT "wanted_concerts".* FROM "wanted_concerts" ORDER BY "wanted_concerts"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<WantedConcert id: 21, department: 0, user_id: 1, created_at: "2018-03-04 13:02:16", updated_at: "2018-03-04 13:02:16">
irb(main):005:0> c.department.class
=> Integer
irb(main):006:0>
I found help.
I did a naughty mistake, I renamed a field in an old migration instead of renaming in a new....
So this may help someone facing a smiliar error...
Related
I am starting a new Rails 7 app and recently found out that we can't use ujs anymore :( I found a plugin called mrujs which is working correctly to send my forms remotely to the server. I am also using stimulus to handle various javascript functions on the page.
The issue that I'm having is my response back after ajax:success processes is not iterable:
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
Below is my HTML, Rails, and Stimulus Code:
HTML
<%= form_with model: Article.new, html: { data: { remote: true, type: "html", "action": "ajax:success->modal-forms#onPostSuccess ajax:error->modal-forms#onPostError" } } do |f| %>
<%= f.label :title, "Article Title" %>
<%= f.text_field :title, class: "form-control" %>
<%= f.submit "Save Article", class: "btn btn-primary" %>
<% end %>
Ruby / Rails
def create
#article = Article.create(article_params)
if #article.errors.any?
render partial: 'error', article: #article, status: :bad_request
else
render #article
end
end
This returns a basic html page that would be inserted into another location within the page.
<li><%= #article.title %></li>
Stimulus Action
onPostSuccess(event) {
event.preventDefault()
const [data, status, xhr] = event.detail
// This is where I get the issue of 'Not Iterable'
}
event.detail gives me the not iterable error. Does anyone know how I can get my response from rails to be formatted so the [data, status, xhr] section will actually work?
If hotwire or turbo is needed for this an example would be extremely helpful :)
Thanks
Eric
This may not be the correct way but it does work:
html
<div data-container="remote">
<%= form_with model: Person.new, html: { data: { "action": "submit->remote#submit" } } do |f| %>
<%= f.text_field :first_name %>
<%= f.submit :submit %>
<% end %>
</div>
peole rails controller
def create
#person = Person.new(person_params)
if #person.save
render json: #person
else
render partial: 'people/person', locals: { person: #person }, status: :unprocessable_entity
end
end
remote stimulus controller
submit(event) {
event.preventDefault()
fetch(event.target.action, {
method: 'POST',
body: new FormData(event.target),
}).then(response => {
console.log(response);
if(response.ok) {
return response.json()
}
return Promise.reject(response)
}).then(data => {
console.log(data)
}).catch( error => {
console.warn(error)
});
}
I currently have this hash,
{ "meal" => "Bomb mi ",
"meal_type " => "Bombwiches",
"times_served" => "8AM to 3PM",
"days_served" => "Tuesday - Sunday",
"notes" => "Hoisin glazed shredded pork or tofu, fresh jalapenos, house-made pickled daikon & carrots, pickles, fresh cilantro, sriracha and a fried egg. ",
"price" => "13.25" }
that I'm able to access in irb via #menu_items[i]
The data is coming from a csv that i'm loading using Sinatra
get "/menu" do
data_file = 'rabbit-sinatra.csv'
#menu_items = []
CSV.foreach(data_file, headers: true, :encoding => 'utf-8') do |row|
#menu_items << row.to_hash
end
erb :menu
end
I'm then looping through this and pulling out any and all necessary values in erb.
<% for i in 0..70 do %>
<% if #menu_items[i]["meal_type"] == "Bombwiches" %>
<div class="menu">
<div class="foo"> <%= #menu_items[i]["meal"]%> </div>
<div class="times_served"> <%= #menu_items[i]["times_served"]%> </div>
<div class="days_served"> <%= #menu_items[i]["days_served"]%> </div>
<div class="notes"> <%= #menu_items[i]["notes"]%> </div>
<div class="price"> <%= #menu_items[i]["price"]%> </div>
</div>
<% end %>
<% end %>
Although #menu_items[i]["notes"] and all other hash parsing commands work, for some reason the initial "meal" value is not being picked up when i run
#menu_items[i]["meal"]
Instead of returning the value for meal "meal" => "Bomb mi " it returns nothing.
Not sure what I'm missing.
Cannot reproduce.
i = 1
#menu_items[i] =
{ "meal" => "Bomb mi ",
"meal_type " => "Bombwiches",
"times_served" => "8AM to 3PM",
"days_served" => "Tuesday - Sunday",
"notes" => "Hoisin glazed shredded pork or tofu, fresh jalapenos, house-made pickled daikon & carrots, pickles, fresh cilantro, sriracha and a fried egg. ",
"price" => "13.25" }
puts #menu_items[i]["price"] #=> "13.25"
puts #menu_items[i]["meal"] #=> "Bomb mi "
Clearly it doesn't contain what you think it does.
As a noob, I have done this a few times, but I always struggle with the strong params. I rolled a new model and controller for uploading a link to a video.
Here is my code:
new_vids_controller.rb
class NewVidsController < ApplicationController
def index
#vids = NewVid.all
end
def new
#vid = NewVid.new(vid_params)
end
def create
if current_user
#vid=NewVid.new(vid_params)
#vid.user_id = current_user.id
if #vid.save
flash[:notice] = "Video Submitted"
redirect_to videos_path
else
flash[:error] = "Error: Vid not Submitted"
redirect_to videos_path
end
else
flash[:error] = "You Must Be Signed In To Submit Videos!"
store_location
redirect_to signin_path
end
end
end
private
def vid_params
params.require(:new_vid).permit(:vid_link, :vid_body, :user_id)
end
new.html.erb
<%= form_for #vid do |f| %>
<%= f.text_field :vid_link, title: "Video Link", placeholder: "Video Link" %>
<%= f.text_field :vid_body, title: "Description", placeholder: "Video Description" %>
<%= f.submit "Submit Link", class: "button tiny" %>
Thanks for helping a noob!
Your new action should be:
#vid = NewVid.new
There are no params to validate there.
I am attempted to display the next three future events from my database but the code below displays nothing. I can't see what I have done wrong.
This is the event controller:
class EventsController < ApplicationController
def show
#event = Event.where(:slug => params[:slug]).first
#future_events = Event.where('end_date > ?', Date.today).order('end_date ASC').limit(3)
General.first.get_blog
General.first.get_twitter
if #event.nil?
#event = Event.first
end
#days = [
{ speakers: #event.sessions.day1_speakers, workshops: #event.sessions.day1_workshops },
{ speakers: #event.sessions.day2_speakers, workshops: #event.sessions.day2_workshops }
]
end
end
And this is the event view:
<% #future_events.first(3).each do |e | %>
<div class="fourcol aboutColumn">
<h3><%= e.title %></h3>
<p><%= e.start_date.strftime("%e %B %Y") %>, <%= e.venue_title %></p>
<p><%= e.event_description %></p>
</div>
<% end %>
You should structure your query to return only the events you need:
Event.where('end_date > ?', Date.today).order('end_date ASC').limit(3)
Beyond that, I can't see why nothing is displayed. Can you post your entire controller method?
Update:
This is the equivalent query for Mongoid:
Event.where(:end_date.gt => Date.today).sort(:end_date => 1).limit(3)
I've searched extensively for this (maybe I'm searching it wrong), but I can't seem to find any answers on this. My aim is to iterate through a bunch of comments in my database and output the info to my view. Unfortunately, my .each do block does not seem to be working.
models/comment.rb
class Comment < ActiveRecord::Base
attr_accessible :content, :photo_id, :user_id
belongs_to :user
belongs_to :photo
.
controllers/photos_controller.rb
def show
#photos = Photo.all
#photo = Photo.find(params[:id])
#user_likes = user_likes(#photo.id)
#comments = Comment.find_all_by_photo_id(#photo.id)
.
views/photos/show.html.erb
<% if !#comments.blank? %>
<% #comments.each do |c| %>
<%= link_to c.user.name, c.user_id %><br />
<%= c.content %>
<% end %>
<% end %>
.
If I use #comments.each do |c|, c.(anything), it will return nothing. I've verified this in rails console as well. However, #comments.first.content or #comments.first.user.name will return what I'm looking for. Why does .first allow me to return content and user fields, but .each do |c| returns nothing?
Here's output from the rails console:
1.9.3-p385 :072 > #comments.each do |c|
1.9.3-p385 :073 > c.content
1.9.3-p385 :074?> end
=> [#<Comment id: 1, user_id: 8, photo_id: 27, content: "lalala", created_at: "2013-04-09 15:36:22", updated_at: "2013-04-09 15:36:22">, #<Comment id: 2, user_id: 8, photo_id: 27, content: "abcd", created_at: "2013-04-09 15:42:00", updated_at: "2013-04-09 15:42:00">, #<Comment id: 3, user_id: 8, photo_id: 27, content: "abadsf", created_at: "2013-04-09 15:58:33", updated_at: "2013-04-09 15:58:33">, #<Comment id: 4, user_id: 8, photo_id: 27, content: "asdf", created_at: "2013-04-09 15:59:12", updated_at: "2013-04-09 15:59:12">]
1.9.3-p385 :075 > #comments.first.content
=> "lalala"
The output you posted from the console is normal:
You are iterating over each comments (so that the c variable in the block contains a comment)
Then you are just calling the attribute content on that instance without doing anything
So the output is the content of the variable #comments.
Now if you call puts c.content you will have what you're expecting.
Remarks:
If you're using Rails 3 (as mentioned by the tags of this question) you shouldn't use the find_by_all_ helpers but the where() method (You should have a look at this free railscast: http://railscasts.com/episodes/202-active-record-queries-in-rails-3).
You should use the Rails relations so that instead of doing Comment.find_all_by_photo_id(#photo.id) you could do #photo.comments.
You could then use this in your view:
<% if #photo.comments.present? %>
<% #photo.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user_id %><br />
<%= comment.content %>
<% end %>
<% end %>