using link_to displays path as link - html

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.

Related

Problem with getting data from html in RoR controller

I'm trying to make a product cart.
I want to be able choose the quantity of product when i add the product to cart.
This is LineItemsController
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
before_action :set_cart, only: [:create]
def index
#line_items = LineItem.all
end
def show
end
def new
#line_item = LineItem.new
end
def edit
end
def create
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product)
#line_item.quantity = params['q'].to_i
if #line_item.save
redirect_to #line_item.cart, notice: 'Item added to cart.'
else
render :new
end
end
def update
if #line_item.update(line_item_params)
redirect_to #line_item, notice: 'Line item was successfully updated.'
else
render :edit
end
end
def destroy
#cart = Cart.find(session[:cart_id])
#line_item.destroy
redirect_to cart_path(#cart), notice: 'Item successfully removed.'
end
private
def set_line_item
#line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :quantity)
end
end
This is view/line_item/_form.html.erb
<%= simple_form_for(#line_item) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
This is view/product/show.html.erb
<% content_for :body_class, 'bg-light' %>
<section class="section instrument-show">
<div class="columns">
<div class="column is-8">
<%= render 'shared/breadcrumb', category: #product.category %>
<h1 class="title is-2"><%= #product.title %></h1>
<!--ul class="pv1">
<%# if #product.brand? %>
<li class="inline-block pr3"><%#= #product.brand %></li-->
<%# end %>
<!--li class="inline-block pr3"><%#= #product.model %></li-->
<%# if #product.condition? %>
<!--li class="inline-block pr3"><%#= #product.condition %></li>
<%# end %>
</ul-->
<div class="feature-image">
<%= image_tag(#product.image_url(:default).to_s) %>
</div>
<div class="content pa4 mt3 bg-white border-radius-3">
<h3 class="subtitle is-4">Description</h3>
<%= #product.description %>
<h3 class="subtitle is-4 pt5">Product Specs</h3>
<table class="table is-narrow">
<% if #product.category.present? %>
<tr>
<td class="has-text-weight-bold">Category:</td>
<td><%= link_to #product.category.name, #product.category %></td>
</tr>
<% end %>
<%# if #product.finish %>
<tr>
<td class="has-text-weight-bold">Finish:</td>
<td><%#= #product.finish %></td>
</tr>
<%# end %>
<tr>
<td class="has-text-weight-bold">Model:</td>
<td><%#= #product.model %></td>
</tr>
</table>
</div>
</div>
<div class="column is-3 is-offset-1">
<div class="bg-white pa4 border-radius-3">
<h4 class="title is-5 has-text-centered"><%= number_to_currency(#product.price) %></h4>
<p class="has-text-centered mb4">Sold by <%#= #product.user.name %></p>
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
<%= button_to 'Add to cart', line_items_path(product_id: #product), class: 'button is-warning add-to-cart' %>
</div>
</div>
</div>
<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_admin_product_path(#product), class: 'button' %>
<% end %>
</section>
Now in this string #line_item.quantity = params['q'].to_i from
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
i'm getting "nil" and i desperately don't now what should i do to get actual number.
P.S. This is view/line_items/new.html.erb
<h1>New Line Item</h1>
<%= render 'form', line_item: #line_item %>
<%= link_to 'Back', line_items_path %>
new.html.erb is to enter your data and if you submit, the data will flow to create method inside your controller, meanwhile show is just to show the data, and you cannot input form inside show,
my suggestion try to put q part of new.html.erb, and put it inside simple_form_for tag
since it is number, you can use number_field rails tag helper as sample below, with name :q and range between 1 .. 50 with step 1, below is the sample probably can help your problem
when you submit from new.html.erb, you will receive params[:q] as part of parameters and save it with create method
<%= simple_form_for(#line_item) do |f| %>
<%= f.error_notification %>
<%= number_field(:q, in: 1.0..50.0, step: 1) %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

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>

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.

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

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