Showing an array of messages in my blog app - ruby-on-rails-4

I am building an blogapp and allowed users to comment to the post with their name and if the user is not signed in it will show as anonamys. Its working but in my browser it is showing an array full of comments. How do i get rid of that. In my Comments Controller
def create
params.permit!
#post = Post.find(params[:post_id])
#comment.user_id = current_user.id if current_user || nil
#comment.save
redirect_to post_path(#post)
end
In the form partial of comments
<%= form_for [#post, #post.comments.build] do |f| %>
In my Posts Controller Show Action
<h4><%= #post.comments.count %> Comments</h4>
<%= render partial: "comments/comment" %>
<%= render partial: "comments/form" %>
And in my Comments Partial I have
<%= #post.comments.each do |comment| %>
<p class="comment_name"><strong><%= (comment.user.user_name if comment.user) || "Anonymous" %></strong></p>
<p class="comment_body"><%= comment.body %></p>
<p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
The array message in my browser is showing as
[#<Comment id: 13, name: nil, body: "nice pic", post_id: 17, created_at: "2015-09-27 20:54:22", updated_at: "2015-09-27 20:54:22", user_id: 8>, #<Comment id: 14, name: "sss", body: "sssss", post_id: 17, created_at: "2015-09-27 21:41:13", updated_at: "2015-09-27 21:41:13", user_id: nil>]
I have no clue where the mistake occurs. Thanks in advance.

The problem is in this line
<%= #post.comments.each do |comment| %>
which should be
<% #post.comments.each do |comment| %>
Notice the change <%= %> to <% %>.
<% %> - execute the statement.
<%= %> - prints the output.

Related

Ransack: boolean attributes are ignored, all records are returned instead

I'm using ransack to allow users do dynamical searching:
Controller:
#q = Visit.search(params[:q]);
#visits = #q.result(distinct: true)
#q.build_condition
Model:
def self.ransackable_attributes auth_object = nil
(column_names - UNRANSACKABLE_ATTRIBUTES) + (_ransackers.keys)
end
View:
<%= search_form_for #q, url: doctor_visits_path do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c%>
<% end %>
<p><%= link_to_add_fields "Add condition", f, :condition %>
<div class="actions"><%= f.submit "Search", {:class => "btn"} %></div>
<% end %>
Partial:
<div class="field">
<%= f.attribute_fields do |a| %>
<%= a.attribute_select({ associations:
[:specialists, :treatment_factors]
},{:class => "form-control"}) %>
<% end %>
<%= f.predicate_select({},{:class => "form-control"}) %>
<%= f.value_fields do |v| %>
<%= v.text_field :value,{:class => "form-control"} %>
<% end %>
<%= link_to "Remove", '#', class: "remove_fields" %>
</div>
I have several boolean attributes in Visits table. When I choose the boolean attribute in the first field, true or false in second (predicate field) and leave the third value field empty, the params are generated correctly, but the sql query is not, the condition is ignored and all the records from the table are returned instead.
What can be the reason?
Add in your model:
Simple version, using PostgreSQL:
ransacker :predicate do
Arel.sql("to_char(\"#{table_name}\".\"predicate\", '99999')")
end
and the same, using MySQL:
ransacker :predicate do
Arel.sql("CONVERT(#{table_name}.id, CHAR(4))")
end

undefined method `send_activation_email'

after a few additions to my code (that didn't actually touch any of the code that's raising the issue now) a specific part of my sign-up process stopped working.
This part is from Michael Hartl's tutorial, and it had worked fine but now is raising me a problem whenever I try to sign-up a new user through a normal sign-up form (I added signups through FB G+ and Linkedin and they all work).
I get a undefined method `send_activation_email' when submit the form. I didn't check the method for some time as I was adding the other signups for social media, but these have their own paths and work through the sessions controller, not the users controller so they shouldn't really create a problem.
In my user model I have:
def send_activation_mail
UserMailer.account_activation(self).deliver_now
end
Then in my users controller:
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
And the view that generates the form:
<div class="row center">
<div class="col-md-4 center mg-top col-md-offset-4">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Re-type your password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create an account", class: "btn btn-primary margin-25" %>
<% end %>
</div>
</div>
Whenever I sign-up a new user, the post action goes through and the user is actually created but I get this Action Controller error:
undefined method `send_activation_email' for #<User:0x007f90064bd5a0>
Extracted source (around line #19):
17
18
19
20
21
22
#user = User.new(user_params)
if #user.save
#user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
A read-out from ruby's console tells me the user has been created (and the error is raised in the conditional if-save ...):
#<User id: 133, first_name: "Txe", last_name: "Pah", email: "teste#email.com", password_digest: "$2a$10$PLY4ZleCtUO0cLdRjIErBuF0j46YQBq1g3Krsf5T.i8...", created_at: "2015-07-25 03:41:49", updated_at: "2015-07-25 03:41:49", remember_digest: nil, admin: false, activation_digest: "$2a$10$voHnNYgzI2jazwRK3EiM6uKsel8Fieb.9HEVs29X8ml...", activated: false, activated_at: nil, reset_digest: nil, reset_sent_at: nil, link: nil, fb: false, fb_id: nil, fb_image: nil, gplus: false, gplus_id: nil, gplus_image: nil, lin: false, lin_id: nil, lin_image: nil, gender: "Unidentified", avatar: nil>
What I don't get is why the send_activation_email is not working as it is defined in the User model.
Could somebody help me or has experienced the same problem?
You have send_activation_mail in User model not send_activation_email. Changing it to
#user.send_activation_mail
should solve your problem.

param is missing or the value is empty:

Here is booking controller:
class BookingsController < ApplicationController
before_action :logged_in_user, only: [:create]
def new
#booking = Booking.new
end
def index
#bookings = Booking.all
end
def create
#booking = current_user.bookings.build(booking_params)
if #booking.save
flash[:success] = "Booking created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def show
#booking = Booking.find(params[:id])
end
def destroy
#booking = Booking.find(params[:id])
#booking.destroy
redirect_to bookings_path
end
private
def booking_params
params.require(:booking).permit(:date, :hour, :game)
end
end
When I post after the utf8 and authenticity token I don't understand why "#<\Booking:0x000001061928a8>\" instead of simply "booking" then the nested hash contains date, hour and game which I permit in the controller.
Here is the view:
<%= form_for(#booking) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :day %>
<%= date_field(#booking, :date) %>
<%= f.label :hour %>
<%= number_field(#booking, :hour, in: 8..19) %>
<%= f.label :game %>
<%= select(#booking, :game, [['Singles', 1], ['Doubles', 2]]) %>
<%= f.submit "Book", class: "btn btn-primary" %>
<% end %>
Any help much appreciated.
Ok it was simply that I wasn't using the the f variable of the form builder for the select boxes, originally I was but I'd had problems rendering the select boxes so I'd taken it out. Must have been due to a syntax error.
Here's the working form_for with a date picker and other select boxes:
<div class="field">
<%= form_for(#booking) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :day %>
<%= f.date_field :day %>
<%= f.label :hour %>
<%= f.number_field :hour, in: 8..19 %>
<%= f.label :game %>
<%= f.select :game, [['Singles', 1], ['Doubles', 2]] %>
<%= f.submit "Book", class: "btn btn-primary" %>
<% end %>
</div>

errors messages doesn't work in HTML ruby code

I am using rails 4.1.6 I looked at the active record validations site and follow their direction, but nothing is displayed in the HTML even if there is an error.
However, when I do it in rails console it works.
post = Post.new #create an empty post to test
post.valid? #false
post.errors.messages #this is successfully generate the error message array
However, it doesn't display any error messages in HTML. In fact "#post.errors" doesn't even run
-Ruby code in html
<%= form_for #post, :method => :post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit "Submit" %>
<% if #errors.any? %>
<ul>
<% #errors.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<% end %>
-My PostsController
def create
# post = Post.new(title: params[:post][:title], url: params[:post][:url])
post = Post.new(post_params)
if post.save
redirect_to posts_path
else
#errors = post.errors.messages
redirect_to paths_path
end
end
private
def post_params
params.require(:post).permit(:title, :url)
end
-My post model
class Post < ActiveRecord::Base
validates :title, :length => {maximum: 140, minimum:1}, :presence => true
validates :title, :length => {maximum: 2083, minimum:1}, :allow_blank => true
end
When it comes to errors I do like to have a partial so I can keep my errors the same in the whole app like so:
app/views/shared/_errors.html.erb:
<% if object.errors.any? %>
<h5>The <%= t("activerecord.models.#{object.class.to_s.downcase}") %> could not be saved due to the following errors:</h5>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
And then for the views you want the errors to be displayed, just call:
<%= render 'shared/errors', object: #your_object %>
Hope this helps!
thanks for your help. Got some tip from my teacher it work. Simply use flash[:message] and it worked. Sorry for the trouble everyone
-My controller code
def create
post = Post.new(post_params)
if post.save
redirect_to :back
else
flash[:message] = post.errors.messages
redirect_to :back
end
end
-My HTML code
<%= simple_form_for #post, :method => :post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :url %>
<%= f.text_field :url %>
<%= f.submit "Submit" %>
<%= flash[:message] %>
<% end %>

Ruby on Rails Guides | partial variable - cannot contain nil or be empty

I am following Ruby on Rails Guides and I have an error in rendering partial when I want to create a new article:
First argument in form cannot contain nil or be empty
Marked error is from partial _form.html.erb located in the same place as index:
<%= form_for #article do |f| %>
index.html.erb has link:
<%= link_to 'New article', new_article_path %>
controller:
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
form partial:
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %><br>
</p>
<% end %>
new.html.erb which renders partial:
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
your controller needs a new method
def new
#article = Article.new
end
that way there's an #article object that the form_for can reference.
When you follow a new link, the new action is executed and the form is displayed. It's only when the form is submitted that the create action is executed.