Ruby on Rails guide (blog) error in 5.10 - ruby

I'm learning Ruby on Rails and to begin I'm making this little blog application via http://guides.rubyonrails.org/getting_started.html#showing-articles .
Now I'm at 5.10 where I need to add validation to the form so if the user adds a tittle with a length shorter than 5.
So this is my articles_controller.rb:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
#article = Article.new(article_params)
##article.save
if #article.save
redirect_to #article
else
render 'new'
end
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
And in this view I have an error (new.html.erb):
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
This is the error I get:
I'm new to Ruby and rails so I hope I can get some help.

You didn't initialize #article instance variable, but you try tu use it in new view. You should have:
def new
#article = Article.new
end

Related

LoadError: Unable to autoload constant Review, Failure/Error: #review = Review.new

I am getting the following error in my tests in this Ruby project I am doing. I can't seem to figure out why.
Below is my controller.
class ReviewsController < ApplicationController
def new
#restaurant = Restaurant.find(params[:restaurant_id])
#review = Review.new
end
def create
#restaurant = Restaurant.find(params[:restaurant_id])
#restaurant.reviews.create(review_params)
end
private
def review_params
params.require(:review).permit(:thoughts, :rating)
end
end
Below is my view for the Reviews.
<%= form_for [#restaurant, #review] do |f| %>
<%= f.label :thoughts %>
<%= f.text_area :thoughts %>
<%= f.label :rating %>
<%= f.select :rating, (1..5) %>
<%= f.submit 'Leave Review' %>
<% end %>
I can't seem to figure out what is wrong.
The model for the Review is wrong. My model was referring to the wrong class.
class Restaurant < ApplicationRecord
belongs_to :restaurant
end
When it should have been the following.
class Review < ApplicationRecord
belongs_to :restaurant
end

How to save many items on one form rails?

I need to save many items to Cart on form, user enter quantity one form, and selected items goes to db, but now save only first entered quantity of item. Why?
my form
<%= form_for #cart_item do |f| %>
<% #category.items.each do |item| %>
<%= item.name %>
<%= f.hidden_field :item_id, :value => item.id %>
<%= f.text_field :qty %>
<% end %>
<%= f.submit %>
<% end %>
And controller
cart_items_controller.rb
class CartItemsController < ApplicationController
before_action :set_cart, only: [:create]
def create
#cart_items = CartItem.create(cart_items_params)
#cart_items.cart_id = #cart.id
if #cart_items.save
redirect_to :back
else
render root_path
end
end
private
def cart_items_params
params.require(:cart_item).permit(:id, :qty, :item_id, :cart_id)
end
def set_cart
#cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
#cart = Cart.create
session[:cart_id] = #cart.id
end
end
There are a few problems here. I'll give you a little bump:
<% #category.items.each do |item| %>
<%= item.name %>
<%= f.hidden_field :item_id, :value => item.id %>
<%= f.text_field :qty %>
<% end %>
For each CartItem, this is going to create an input like this
<input name="qty">
This is problematic because only one (the last one in the DOM) will be submitted. You need to research fields_for and incorporate that into your loop in order to get unique names for each Item in the form.
This same issue follows through into your controller
def cart_items_params
params.require(:cart_item).permit(:id, :qty, :item_id, :cart_id)
end
This is going to look for a single :id, :qty, :item_id, and :cart_id, when in reality you're looking to accept multiple :item_id and :qty fields. You need to research Strong Parameters with nested has_many associations.
Finally you have this
#cart_items = CartItem.create(cart_items_params)
which is going to attempt to create a single CartItem when you're really trying to create multiple items and associate them back to the Cart. You need to research accepts_nested_attributes_for as well as more generally "rails form save has_many association". It's a widely covered topic here on SO and elsewhere.
I do this:
def create
#cart_items = params[:cart_items]
#cart_items.each do |c|
#cart_item = CartItem.new(c)
if #cart_item.qty.present?
#cart_item.cart_id = #cart.id
#cart_item.save
end
end
and form
<%= form_tag cart_items_path do %>
<% #cart_items.each do |cart_item| %>
<%= fields_for "cart_items[]", cart_item do |f| %>
<% #category.items.each do |item| %>
<%= item.name %>
<%= f.hidden_field :item_id, value: item.id %>
<%= f.text_field :qty %>
<% end %>
<%= f.submit %>
<% end %>
<% end %>
<% end %>

Can not create user with Rails app on heroku, but works perfect on localhost

So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:

Simple error in my rails app

I have an error in my rails app telling me that the index method is undefined.
I have created a such simple form just for inserting data using the (new method page) only
but it is not working now for some reason
class PeoplesController < ApplicationController
def index
end
def new
#people = People.new
end
def create
#people = People.new(params[:#people])
if #people.save
redirect_to new_people_path
end
end
end
# new.html.haml
<h2>Hello there!</h2>
<hr >
<%= form_for #people do |f| %>
FirstName:<%= f.text_field :first %><br />
LastName:<%= f.text_field :last %><br />
<%= f.submit %>
<% end %>
Here is the error code:
NoMethodError in Peoples#new
Showing /Users/kasim/Desktop/form/app/views/peoples/new.html.erb where line #3 raised:
undefined method `people_index_path' for #<#<Class:0x007fa33da58d58>:0x007fa34031c578>
Extracted source (around line #3):
1: <h2>Hello there!</h2>
2: <hr >
3: <%= form_for #people do |f| %>
4: FirstName:<%= f.text_field :first %><br />
5: LastName:<%= f.text_field :last %><br />
6: <%= f.submit %>
The documentation for form_for says that a form_for call is equivalent to this
<%= form_for #post, :as => :post, :url => post_path(#post), :method => :put, :html => { :class => "edit_post", :id => "edit_post_45" } do |f| %>
...
<% end %>
Notice that the url attribute is calling out to a named route. In your case it appears the named route is people_index_path. I'd run rake routes and make sure that the named route in question really exists.

Sinatra Partial with data?

I am making a super small Sinatra blog application, how could I take entries from a database, format them, and insert them into my layout?
class Blog < Sinatra::Base
helpers do
def partial (template, locals = {})
erb(template, :layout => false, :locals => locals)
end
end
get "/list" do
#posts = Post.all
erb :list
end
end
list.erb:
<% #posts.each do |post| %>
<%= partial(:post, :post => post) %>
<% end %>
post.erb:
<h1><%= post.title %></h1>
<p><%= post.body %></p>
<% #posts.each do |post| %>
<%= erb :"_partial_name", :locals => {} %>
<% end %>
the partial template need start with _

Resources