Ruby on Rails SQL Query in view - mysql

I am working in a Rails Project and my problem is that for some reason the home view shows the SQL Query next to the book image.
Here is the HTML code that shows the index view
<div id="books-index">
<% #books.each_slice(4) do |book| %>
<div class="row">
<%= book.each do |book| %>
<div class="col-md-3 col-sm-3">
<h3><%= book.title %></h3>
<%= image_tag(book.coverpath) %>
<%= link_to 'Read More', book_path(book), class:"btn btn-primary" %>
</div>
<% end %>
</div>
<% end %>
</div>
and the books controller
class BooksController < ApplicationController
def new
#page_title = 'Add Book'
#book = Book.new
#category = Category.new
#author = Author.new
#publisher = Publisher.new
end
def create
#book = Book.new(book_params)
if #book.save
flash[:notice] = "Book Created"
redirect_to books_path
else
render 'new'
end
end
def index
#books = Book.all
end
private
def book_params
params.require(:book).permit(:title, :category_id, :author_id, :publisher_id, :isbn, :price, :buy, :format, :excerpt, :pages, :year, :coverpath)
end
end
Thanks a lot for your help

Remove the = on your book.each iteration:
<% #books.each_slice(4) do |book| %>
<div class="row">
<% book.each do |book| %>
...
<% ... %> just evaluated, not printed.
<%= ... %> evaluated and printed.

Related

I'm having trouble displaying comments on my Rails Blog

I'm currently working on my Rails Blog. While the development is coming along nicely, I've hit a roadblock in the comment.
Every time I try to leave a test comment, I always get "rollback transaction" just before the comment gets saved.
Here's what happens when I create a comment.
Here's the code for looking at blog entry:
post/show.html.erb
<h1 class="title">
<%= #post.title %>
</h1>
<h2>Authored by:
<%= #post.user.username%>
</h2>
<p>
<%=raw #post.content %>
</p>
<div class="comments">
<h3 class="is-3">Comments</h3>
<%if #post.comments.count == 0%>
<strong>There are no comments on this post. Feel free to send one!</strong><br/>
<% else %>
<%= render #post.comments %>
<% end %>
<%if !user_signed_in? %>
<strong>Yo, if you wanna comment on my site, either <%= link_to 'Sign up', new_user_registration_path %> or <%= link_to 'Log in', new_user_session_path %></strong>
<% else %>
<%= render partial: "comments/form", locals: {comment: #post.comments.new} %>
<% end %>
</div>
</div>
<br>
<% if user_signed_in? %>
<div class="functions">
<%= link_to "Update", edit_post_path(#post) if policy(#post).update? do%>
<i class="fa fa-edit editpage fa-3x"></i>
<% end %>
<%= link_to "Delete", #post, :confirm => "Are you sure you want to delete this post?", :method => :delete if policy(#post).destroy? do%>
<i class="fa fa-trash deletepage fa-3x"></i>
<% end %>
</div>
<% end %>
<%= link_to 'Back to the main page', root_path %>
In case you're curious, here's what the comment partial looks like:
<strong><%= #post.comment.user.username %> says:</strong><br>
<%=#post.comment.content %>
<p>
<=time_ago_in_words(comment.created_at) %>
</p>
Can anyone help me figure out why is it that when I create a comment, it rolls back the transaction? I'll provide more information if required.
Edit: Here's the Comments Controller for my rails blog.
class CommentsController < ApplicationController
before_action :set_post
def create
set_post
# Create associated model, just like we did in the console before
#comment = #post.comments.create(comment_params)
# We want to show the comment in the context of the Post
#comment.post_id = #post.post_id
#comment.user_id = current_user.user_id
#comment.save
redirect_to #post
end
def update
#comment = set_comment
#comment.update(comment_params)
redirect_to #post
end
def destroy
#comment = set_comment
#comment.destroy
redirect_to #post
end
private
def comment_params
params.require(:comment).permit(:content)
end
def set_post
#post = Post.friendly.find(params[:post_id])
end
def set_comment
#comment = Comment.find(params[:id])
end
end
Comment Model:
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end
Also, the Comment form
<strong><%= #post.comment.user.username %> says:</strong><br>
<%=#post.comment.content %>
<p><= time_ago_in_words(comment.created_at) %></p>

First argument in form cannot contain nil or be empty

Ive looked at atleast 8 other forums with the same title and none of the ones read helped me at all!
So im using a partial to render a form to a edit.html.erb page and a new.html.erb page. My controller page works except for the edit page!
Here is my code for my controller page :
class ProfilesController < ApplicationController
def new
# form where a user can fill out their own profile.
#user = User.find( params[:user_id] )
#profile = Profile.new
end
def create
#user = User.find( params[:user_id] )
#profile = #user.build_profile(profile_params)
if #profile.save
flash[:success] = "Profile Updated!"
redirect_to user_path( params[:user_id] )
else
render action: :new
end
end
def edit
#user = User.find( params[:id] )
#profile = #user.profile
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :job_title, :phone_number, :contact_email, :description)
end
end
my edit.html.erb page:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Edit Your Profile</h1>
<p class="text-center">Be a part of the Dev Match community and fill out your profile!</p>
<div class="well">
<%= render 'form' %>
</div>
</div>
</div>
my new.html.erb page:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Create Your Profile</h1>
<p class="text-center">Be a part of the Dev Match community and fill out your profile!</p>
<div class="well">
<%= render 'form' %>
</div>
</div>
</div>
and _form.html.erb:
<%=form_for (#profile) , url: user_profile_path do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :job_title %>
<%= f.select :job_title, ['Developer', 'Entrepreneur', 'Investor'], {}, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :contact_email %>
<%= f.text_field :contact_email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Update Profile", class: 'btn btn-primary' %>
</div>
<% end %>
You change your code into this:
def edit
#user = User.find(params[:user_id])
#profile = #user.profile
end
#user is null because the id does not exist in DB.
If you create a new instance of User in the action new you can use the same object in the action create without use the method User.find
As improvement, try to add before_action in your controller.
class ProfilesController < ApplicationController
before_action :prepare_model, except: [:new, :create]
def new
# form where a user can fill out their own profile.
#user = User.new
#profile = Profile.new
end
def create
#profile = #user.build_profile(profile_params)
if #profile.save
flash[:success] = "Profile Updated!"
redirect_to user_path( params[:user_id] )
else
render action: :new
end
end
def edit
#profile = #user.profile
end
private
def prepare_model
#user = User.find(params[:user_id])
raise ActiveRecord::RecordNotFound if #user.nil?
end
def profile_params
params.require(:profile).permit(:first_name, :last_name, :job_title, :phone_number, :contact_email, :description)
end
end

using link_to displays path as link

I am using <% #articles.each do |article| %> do display all articles which is working however it is also displaying '/subs/27/articles' as one of the articles. When I click it it goes to my index view. Why is this link showing here with all the articles
```
class SubsController < ApplicationController
def show
#sub = Sub.find(params[:id])
#category = Category.find(params[:category_id])
#articles = #sub.articles
#article = #sub.articles.build
end
def new
#category = Category.find(params[:category_id])
#sub = Sub.new
end
def create
#category = Category.find(params[:category_id])
#sub = Sub.new(params.require(:sub).permit(:title))
#sub.category = #category
if #sub.save
flash[:notice] = "Subcategory was saved"
redirect_to [#category, #sub]
else
flash[:error] = "The Subcategory could not be saved"
render :new
end
end
def edit
#category = Category.find(params[:category_id])
#sub = Sub.find(params[:id])
end
def update
#category = Category.find(params[:category_id])
#sub = Sub.find(params[:id])
#sub.category = #category
if #sub.update_attributes(params.require(:sub).permit(:title))
flash[:notice] = "Subcategory was saved"
redirect_to [#category, #sub]
else
flash[:error]= "The subcategory could not be saved"
render :edit
end
end
def sub_params
params.require(:sub).permit(:title)
end
views/subs/show.html.erb
<h1 align="center"><%= #sub.title %></h1>
<div class="row">
<div class="col-md-8">
<%= link_to "Back", :back, class: "btn btn-default" %>
<% if user_signed_in? %>
<%= link_to "Edit", edit_category_sub_path(#category, #sub), class: "btn btn-success" %>
<% else %>
<% end %>
</div>
<!--
<div class="col-md-4">
<%= link_to "New Article", new_category_sub_path(#category), class: 'btn btn-success' %>
</div>
-->
</div>
<hr>
<br>
<div class="row">
<% #articles.each do |article| %>
<div class="col-xs-3">
<div class="tile" >
<h5 align="center">
<%= link_to article.title, [#sub, article] %>
</h5>
</div>
</div>
<% end %>
</div>
<% if user_signed_in? %>
<div class="col-md-12">
<%= render 'articles/form', category: #category, article: #article, sub: #sub %>
</div>
<% else %>
<% end %>
<!--
<div class="col-md-12">
<%= render 'articles/form', category: #category, article: #article, sub: #sub %>
</div>
-->
```
After you gave me more information,
I think this is your problem right here:
#sub.articles.build
It is building a new unsaved article association and then later it iterates through articles, including that unsaved record, hence why the title is empty.

index outputs content that isn't in the .html.erb file

I'm following a ruby on rails tutorial to build a simple application using database tables and for some reason my home page outputs more than it should. It creates this array that looks like a database query made with prompt and puts it on the page. I'm pretty sure the problem is with my index.html.erb file (if I empty the file and reload the weird content isn't there), but I can't figure out what's going on. Here's a screenshot of what happens.
controller code:
class BooksController < ApplicationController
def new
#page_title = 'Add Book'
#book = Book.new
#category = Category.new
#author = Author.new
#publisher = Publisher.new
end
def create
#book = Book.new(book_params)
#book.save
redirect_to books_path
end
def update
end
def edit
end
def destroy
end
def index
#books = Book.all
end
def show
end
private
def book_params
params.require(:book).permit(:title, :category_id, :author_id, :publisher_id, :isbn, :price, :buy, :format, :excerpt, :pages, :year, :coverpath)
end
end
html:
<div id= "books-index">
<% #books.each_slice(4) do |book| %>
<div class = "row">
<%= book.each do |book| %>
<div class="col-md-3 col-sm-3">
<h3><%= book.title %></h3>
<%= image_tag(book.coverpath) %>
<%= link_to 'Read More', book_path(book), class:'btn btn-primary' %>
</div>
<% end %>
</div>
<% end %>
</div>
I am new to ruby and ruby on rails, so if I need to post any more resources or info in order to make my question clearer please let me know. Thanks for the help
You should put book.each call inside of <% tag, instead of <%= tag:
<% books.each do |book| %>
Now, you print the result of books.each (which is books array) into your HTML.
<%= book.each do |book| %>
The equal after the opening syntax prints the command(s) that follow. That should be the case, try removing it
remove= from the line <%= book.each do |book| %>

Issues getting Microposts in Ruby on Rails to post to a followers wall

I was following the Michael Hartl tutorial for creating a basic twitter type app, and I am trying to figure out how to get my posts to show up on a follower's wall rather than always posting to my own. After going through my code and various other answers, I believe the mistake lies with in the forms tags I set up inside of my HTML... Is there some very noticeable mistake? Or maybe a helpful tutorial on the topic that you know of?
<div class="user-post">
<%= form_for(#micropost) do |f| %>
<div>
<%= f.text_area :content, placeholder: "Compose new micropost...", class: 'create-post-input' %>
<div>
<%= f.submit "Post", class: 'post-button-light' %>
</div>
</div>
<% end %>
<div>
<div class= "scrollBox" >
<% if #user.microposts.any? %>
<% #microposts.each do |micropost| %>
<li>
<%= micropost.content %>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete %>
<%end %>
</li>
<% end %>
<%end%>
</div>
</div>
Controllers:
def show
#micropost = current_user.microposts.build if signed_in?
#feed_items = current_user.feed.paginate(page: params[:page])
#user = User.find(params[:id])
#microposts = #user.microposts.paginate(page: params[:page])
end
def following
#title = "Following"
#user = User.find(params[:id])
#users = #user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
#title = "Followers"
#user = User.find(params[:id])
#users = #user.followers.paginate(page: params[:page])
render 'show_follow'
end
Micropost controller:
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
redirect_to current_user
else
redirect_to current_user
end
end
def destroy
#micropost.destroy
redirect_to current_user
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
#micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if #micropost.nil?
end