Why is post title not showing up? - ruby-on-rails

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

Related

why the parameter come out in my UI page?

i have no idea why the parameter suddenly come out at my role create page. After i add the permission checkbox there then it appear.
new.html.erb
<% provide(:title, "Create Roles") %>
<h1 class="dashboard">Create Role</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #role, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= #permissions.each do |permission|%>
<%= check_box_tag 'permission_ids[]', permission.id %>
<%= f.label :permission_name, permission.name %>
<% end %>
<%= f.hidden_field :company_id , value: 2%>
<%= f.submit "Create Role", class: "btn btn-primary bottom" %>
<% end %>
</div>
</div>
Parameter show at role create page
You are currently seeing the string output of the each method because you're using <%= #permissions.each ...
Instead use the "silent" <% as you do for end:
<% #permissions.each do |permission| %>

Rails Turbo Frame Tag wrong number of arguments error

I've upgraded to rails 6.1 from rails 6.0 and trying out Hotwire, but I am getting an error when trying to run my application:
wrong number of arguments (given 0, expected 1..4)
Extracted source (around line #1):
<%= turbo_frame_tag 'branch' do %>
my code in my edit.html.erb:
<%= turbo_frame_tag 'post' do %>
<h3>Edit post</h3>
<%= render 'form', post: #post %>
<% end %>
my code in my _form.html.erb:
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<h5><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h5>
<ul class="browser-default">
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div class="form-field">
<div class="input-field">
<%= form.text_field :title %>
<%= form.label :title %>
</div>
</div>
<br>
<div class="form-field">
<div class="input-field">
<%= form.text_field :description %>
<%= form.label :description %>
</div>
</div>
<div class="actions"><%= submit_tag "Submit", :type=>"submit", :class=>"btn", :data => { :disable_with => "Processing" } %></div>
<% end %>
my code in my show.html.erb:
<%= turbo_frame_tag 'branch' do %>
<h3><%= #post.title %></h3>
<p><%= #post.description %></p>
<% end %>
Does anybody have an idea what I might have missed during the upgrade process? I ran rails turbo:install and also rails hotwire:install or am I missing something in the turbo frame tag?

How to hide database records from view?

I'm beginner in Ruby On Rails and I'm reading "Getting Started with Rails" now.
I created models and controllers for Article and Comments, everything ok, I'm able to add comments and they appear on Article view, but besides I see records from database in this format:
[# Comment id: 1, commenter ... updated_at: "2016-05-20 09:26:25"]
Why they appear and how to hide it? Code of Article's view show.html.erb:
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= #article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>>
<%= comment.body %>
</p>
<% end %>
<h3>Add a comment</h3>
<%= form_for([#article, #article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(#article) %>
<%= link_to 'Back', articles_path %>
Instead of:
<%= #article.comments.each do |comment| %>
It should be:
<% #article.comments.each do |comment| %>
<%= %> used to render something. And you are iterating an object so you need to use <% %>.
Change this:
<% #article.comments.each do |comment| %>
What <%= does is displaying whatever data he's dealing with. If you do <% #article.comments.each do |comment| %> it will deal with the data as well but he won't display anything. And then you can display whatever you want with:
<%= comment.commenter %>
<%= comment.body %>

How can I separate my pics and videos so only the one the user post is displayed?

I am trying to write an if else statement so that when a users post a photo only the photo is displayed the same goes for videos. Right now if a users post a video a blank photo will be displayed under the video as well. My code is below. I know it has something to do with the lines:
<%= image_tag #post.image.url(:medium) %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
I just don't know what the best way is to only show the one the user is posting.
Post/Show.html
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<%= image_tag #post.image.url(:medium) %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
</div>
<div class="panel-body">
<p><%= #post.description %></p>
<p><strong><%= #post.user.name if #post.user %></strong></p>
<% if #post.user == current_user %>
<%= link_to edit_post_path(#post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
Form
<%= form_for #post, html: { multipart: true } do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :image %>
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :video %>
<%= f.file_field :video %>
</div></br>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-danger btn-md" %>
</div>
<% end %>
if i understand you correctly, you could try this...
<% if #post.respond_to?(:image) %>
<%= image_tag #post.image.url(:medium) %>
end
<% if #post.respond_to?(:video) %>
<%= image_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
end
You are basically checking for the existence of the particular attribute. You are saying, if it is here, if it exists, then display it.
Or you could try this
<% if #post.image.url != nil %>
<%= image_tag #post.image.url(:medium) %>
.........(repeat for other attributes)
<% end %>
or this
<% unless #post.image.url == nil %>
<%= image_tag #post.image.url(:medium) %>
<% end %>
see here for more info.
There are many ways to achieve what you want. this kind of thing is one of the fundamental concepts of programming.

adding categories for posts rails

I'm trying to add catagories to my posts with a has_and_belongs_to_many relationship
I"m pretty close but just a little off.
here is my post index.html.erb
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Post Index<small> by title</small></h1>
</div>
</div>
<%= form_tag posts_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<center><%= link_to 'Create New Post', new_post_path %></center>
</p>
<p><% #posts.sort_by(&:comments_count).reverse.each do |post| %></p>
<p><%= image_tag avatar_url(post.user), size: "31x31" %> <%= post.user.name %></p>
<p><strong>Title: </strong><%= post.title %></p>
<p><strong>Summary: </strong><%= post.summary %></p>
<p><%= post.catagories.name %>
<p><%= link_to 'Read Full Post', post %> Total Comments for post . . . (<%= post.comments.count %>)</p>
<p><strong>Posted ON: </strong><%= post.created_at.strftime('%b %d, %Y') %></p>
<br>
<p><% if current_user.admin? %><%= link_to "delete", post, method: :delete, data: { confirm: "You sure" } %>
<% end %>
<% if current_user.admin? %><%= link_to 'Edit', edit_post_path(post) %><% end %></p>
<% end %>
</div>
catagory.rb file
class Catagory < ActiveRecord::Base
has_and_belongs_to_many :posts
end
post.rb file
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
has_and_belongs_to_many :catagories
mount_uploader :image, ImageUploader
searchable do
text :title, :boost => 5
text :content
end
def comments_count
comments.count
end
end
post _form.html.erb
<%= form_for #post, :html => {:multipart => true} do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :summary %><br>
<%= f.text_field :summary %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
right now in the new it just says Catagory in stead of one of the three catagories I set in my rails console in my post index.html.erb file
#<ActiveRecord::Relation [#<Catagory id: 1, name: "lolcats", created_at: "2013-11-20 15:17:05", updated_at: "2013-11-20 15:17:05">, #<Catagory id: 2, name: "surfing", created_at: "2013-11-20 15:17:42", updated_at: "2013-11-20 15:17:42">, #<Catagory id: 3, name: "dance", created_at: "2013-11-20 15:18:11", updated_at: "2013-11-20 15:18:11">]>
the check box comes up fine in my post _form though, anyone feel like taking a stab at it?
according to this rails cast you need to change check_box_tag here
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
to
<%= check_box_tag "post[catagory_ids][]", catagory.id, #post.catagories.include?(catagory) %> #why catAgory, by the way?

Resources