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

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| %>

Related

Ruby on Rails SQL Query in view

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.

Rails: Get form to only render when certain conditions are met

Im trying to get this loop to only render the reviews form for services which doesnt already have a review. I can't get it to function properly. Any ideas?
<% #services.each do |service| %>
<% if service == #booked && !#hasReview %>
<%= form_for(service, service.reviews.new) do |f| %>
<label>Create review for</label> <%= label_tag service.title %>
<div id="user_stars"></div>
<div class form-group>
<%= f.text_area :comment, rows: 3, class: "form-control" %>
</div>
<%= f.hidden_field :service_id, value: service.id %>
<div class="actions">
<%= f.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
<% end %>
The #booked and #hasReview actions are working correctly by themselves. So I guess Im setting it up wrongly with the IF
EDIT:
services_controller.rb
def show
#user = User.find(params[:id])
#services = #user.services
#booked = Booking.where("service_id = ? AND user_id = ?", #service.id, current_user_id).present? if current_user
#reviews = #service.reviews
#hasReview = #reviews.find_by(user_id: current_user_id) if current_user
end
reviews_controller.rb
def create
#review = current_user.reviews.create(review_params)
redirect_to request.referer
end
Your logic looks good, so I'm guessing your problem is with this part of your if statement:
if service == #booked
Judging from this line in your services_controller:
#booked = Booking.where("service_id = ? AND user_id = ?", #service.id, current_user_id).present? if current_user
it looks like the #booked instance variable is a boolean. So you're comparing a boolean to service, but it doesn't look like service is a boolean.
So you'll want to change the if statement so that you're comparing two equivalent types, and in your case you probably want to compare two boolean values.

Search box in ruby on rails not displaying results

I'm working with Ruby on Rails, trying to get my search bar to display the results. I've tried searching for similar issues, but most already had search working and weren't getting it to work right.
in my html I have:
<%= form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<div class="scrollBox">
<%= will_paginate%>
<ul>
<% #users.each do |user| %>
<li>
<%= link_to user.name, user, {:class=>"signout-style"} %>
</li>
<% end %>
</ul>
<%= will_paginate %>
</div>
In my users controller I have:
def index
#users = User.paginate(page: params[:page])
#searches = User.search(params[:search])
end
In my users model I have:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
I was using the railscast episode #37
Any help is appreciated!
Update the index action as below:
def index
#users = User.search(params[:search]).paginate(page: params[:page])
end
Currently, you are storing the search results in instance variable #searches with this line:
#searches = User.search(params[:search])
BUT in your view you are accessing #users which is set as #users = User.paginate(page: params[:page]). So, you always see all the users in the view and not the searched users.
UPDATE
As you are using Rails 4.0.2, I would suggest you to refactor your search method as below:
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
all
end
end

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

Placing link at top

How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?