undefined method `bookings_path' - ruby

I have a form to fill in booking details, but I'm getting the following error:
NoMethodError in Bookings#new
undefined method `bookings_path'
This happened after change the routes.rb file, to nest the booking resources in the user resource.
My form file code is the following:
<% provide(:title, 'Book Now!') %>
<section id="book-now">
<%= form_for(#booking) do |f| %>
<header>
<h1>Edit booking</h1>
</header>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.text_field :check_in, placeholder: "Check-in" %>
<%= f.text_field :check_out, placeholder: "Check-out" %>
<%= f.submit "Save" %>
<% end %>
</section>
My booking controller code is:
class BookingsController < ApplicationController
def new
#booking = Booking.new
end
def create
#booking = Booking.new(params_bookings)
#booking.user_id ||= current_user.id
if #booking.save
redirect_to user_path(#booking.user_id)
else
# render 'new'
end
end
end
private
def params_bookings
params.require(:booking).permit(:check_in, :check_out, :name, :user_id)
end
end
and my routes.rb file looks like this:
Hightide::Application.routes.draw do
resources :users do
resources :bookings
end
match '/users/:user_id/bookings/new', to: 'bookings#new', via: [:post, :get]

you have this error because your bookings routes are nested to the users, so when you write this
form_for #booking
you essentially call
form_for bookings_path, ...
coz rails gets the type of the object you send to form_for and tries to get vanilla path for it.
to solve the problem you need either create the vanilla bookings resources in your routes, or specify both a user and a booking reference for the form_for call, like so
form_for [current_user, #booking]

Related

cant upload images rails and error occur

I want to upload an image to my localhost using gem 'carrierwave', i have painting and galleries controller like below code
painting controller
class PaintingsController < ApplicationController
def index
#paintings=Painting.all
end
def new
#painting=Painting.new
end
def show
#painting=Painting.find(params[:id])
end
def create
#byebug
#painting=Painting.new(painting_params)
if #painting.save
flash[:success]="Created image in album"
redirect_to gallery_path(#painting)
else
flash[:error]="Fail!"
render 'new'
end
end
private
def painting_params
params.require(:painting).permit(:name,:gallery_id)
end
end
Gallery controller
class GalleriesController < ApplicationController
def index
#galleries=Gallery.all
end
def new
#gallery=Gallery.new
end
def show
#gallery=Gallery.find(params[:id])
end
def create
#gallery=Gallery.create!(gallery_params)
redirect_to galleries_path
end
private
def gallery_params
params.require(:gallery).permit(:name)
end
end
ok then 2 model files :
gallery.rb
class Gallery < ApplicationRecord
has_many :paintings
end
painting.rb
class Painting < ApplicationRecord
def access_params
params.require(:painting).permit(:gallery_id, :name, :image)
end
belongs_to :gallery, optional: true
mount_uploader :image, ImageUploader
end
It seems everying goes well but then i stuck at the step showing image on show.html.erb in gallery.
show.html.erb
<div id="paintings">
<% #gallery.paintings.each do |painting| %>
<div class="painting">
<%= image_tag painting.image_url.to_s %>
<div class="name"><%= painting.name %></div>
<div class="actions">
<%= link_to "edit", edit_painting_path(painting) %> |
<%= link_to "remove", painting, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<div class="clear"></div>
</div>
the image isn't showed up althought flash in gallery 's controller reported that i created the image,i inspected the website then i tried print painting s'attributes on show.html.erb
<%= #gallery.name %>
<%= #gallery.paintings.name %>
<%= #gallery.paintings.gallery_id%>
<%= #gallery.paintings.image%>
Only gallery's name and painting's name are printed out. other two methods has an error occur.
undefined method `gallery_id' for #<ActiveRecord::Associations::CollectionProxy []>
I don't know why gallery can only access to painting's name but not others two.I searched for this error but i dont think those situation apply to mine . What is the problem guys?
Your "paintings" is a collection, not a single image, so you need to either iterate on each of them or select the first one:
<%= #gallery.paintings.first.name %>
<%= #gallery.paintings.first.gallery_id %>
<%= #gallery.paintings.first.image %>
I found my error ! in user controller add :image to method painting_params
def painting_params
params.require(:painting).permit(:name,:gallery_id,:image)
end

Ruby on Rails guide (blog) error in 5.10

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

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:

Save record in rails 4.0 using params hash

I'm trying to create a new record in rails using the form_for helper method. I think the params hash is blank because I keep getting blank errors when I submit the form.
This is my form:
<% provide(:title, "Add Department") %>
<h1>Add Department</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#department) do |f| %>
<%= render 'shared/department_error_messages' %>
<%= f.label :Full_Department_Title %>
<%= f.text_field :full_name %>
<%= f.label :Department_Abbreviation %>
<%= f.text_field :abbreviation %>
<%= f.submit "Add department", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
This is my departments controller
class DepartmentsController < ApplicationController
def show
#department = Department.find(params[:id])
end
def new
#department = Department.new
end
def create
#department = Department.new(params[department_params]) # Not the final implementation!
if #department.save
redirect_to root_path
else
render 'new'
end
end
private
def department_params
# This says that params[:department] is required, but inside that, only params[:department][:full_name] and
# params[:department][:abbreviation] are permitted. Unpermitted params will be stripped out
params.require(:department).permit(:full_name, :abbreviation)
end
end
This is the Model:
class Department < ActiveRecord::Base
validates :full_name, presence: true, length: { minimum: 6 }
end
When I submit, errors are rendered saying the full_name can't be blank (and the fields are now empty). The debug info is:
--- !ruby/hash:ActionController::Parameters utf8: ✓
authenticity_token: EjfYjWAzaw7YqVZAkCPZwiEMFfb2YLIRrHbH1CpZOwA=
department: !ruby/hash:ActionController::Parameters
full_name: Sports Department
abbreviation: SD
commit: Add department
action: create
controller: departments
I've also checked the development log. The transaction starts and is then rolled back. I can save a record in the console so I'm guessing its something to do with the params hash, but can't figure it out. Please help
This line:
#department = Department.new(params[department_params])
Should be:
#department = Department.new(department_params)
And, as a minor issue (but not causing this problem), your label tags should look like:
f.label :full_name, "Full Department Title"
This way they are correctly associated with the input.

Resources