No route matches [POST] "/portfollios/new" - ruby-on-rails-5

Hey Guys I am getting issue, No Route Matches, though I have created both new as well as create method.
portfollios_controller.rb
class PortfolliosController < ApplicationController
def index
#portfolio_items = Portfollio.all
end
def new
#portfolio_item = Portfollio.new
end
def create
#portfolio_item = Portfollio.new(params.require(:portfollio).permit(:title, :subtitle, :body))
respond_to do |format|
if #portfolio_item.save
format.html { redirect_to portfollio_path, notice: 'Your portfolio item is now live.' }
else
format.html { render :new }
end
end
end
end
routes.rb
Rails.application.routes.draw do
resources :portfollios
end
new.html.erb
<h1>Create a new Portfolio Item</h1>
<%= form_with(model: #portfolio_items, local: true) do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :subtitle %>
<%= form.text_field :subtitle %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Can someone help me out with the issue. I am not able to figure it out.

I figured it out. The issue is with with the file new.html.erb
I needed to change below line:-
<%= form_with(model: #portfolio_items, local: true) do |form| %>
to
<%= form_with(model: #portfolio_item, local: true) do |form| %>
It should be #portfolio_item not plural.

Related

Can't edit: undefined method `map' for nil:NilClass

Hello after creating a Product i can't edit it. The form a little complex so its made it i hard to get the edit to work.
Error when i try to edit.
Completed 500 Internal Server Error in 16ms
ActionView::Template::Error (undefined method `map' for nil:NilClass):
17:
18: <p><%= f.link_to_add "Add a image", :product_images, :data => { :product_image => "#product_images" } %></p>
19:
20: <%= f.collection_select :category_id, #categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
21:
22: <% #categories.each do |category| %>
23: <div class='sizes_container' id
Here is the Product form.
<%= javascript_include_tag "custom" %>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_nested_form_for #product do |f| %>
<%= f.fields_for :product_images do |product_image| %>
<% if product_image.object.new_record? %>
<%= product_image.file_field(:product_image) %>
<%= product_image.link_to_remove "Remove Image", data: { confirm: "Are you sure you want to delete this image?" } %>
<% else %>
<%= product_image.hidden_field :_destroy %>
<% end %>
<% end %>
<p><%= f.link_to_add "Add a image", :product_images, :data => { :product_image => "#product_images" } %></p>
<%= f.collection_select :category_id, #categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
<% #categories.each do |category| %>
<div class='sizes_container' id ='sizes_container_for_<%= category.id %>'>
<% category.sizes.each do |size| %>
<%= label_tag size.title %>
<%= f.simple_fields_for :product_sizes do |product_size| %>
<%= product_size.input :quantity %>
<%= product_size.hidden_field :size_id %>
<% end %>
<% end %>
</div>
<% end %>
<%= f.input :title, label:"Title"%>
<%= f.input :price, label:"Price"%>
<%= f.input :description,label:"Description" %>
<%= f.input :size_description, label:"Size Details"%>
<%= f.input :shipping_description, label:"Shipping Details"%>
<%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
<%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
Any ideas? I had a look around the net i couldn't find the answer. So any help would be great. Thank you in advance.
Products controller
class ProductsController < ApplicationController
before_action :set_product, only: [:edit, :show, :update]
before_action :correct_user_edit, only: [:edit, :update, :destroy]
before_action :logged_in_user, only: [:new, :edit, :update, :destroy]
def index
#products = Product.all
end
def new
#product = Product.new
#categories = Category.preload(:sizes).order(:name)
#product.product_images.build
#product.product_sizes.build
end
def home
#products = Product.paginate(page: params[:page])
end
def edit
end
def show
end
def update
if #product.update(product_params)
redirect_to #product
flash[:success] = 'Item was successfully updated.'
else
render "edit"
end
end
def create
#product = Product.new product_params
#product.user_id = current_user.id
if #product.save
redirect_to #product
flash[:success] = "You have created a new product"
else
flash[:danger] = "Your product didn't save"
render "new"
end
end
def destroy
#product.destroy
flash[:success] = "Product deleted"
redirect_to user_products_path
end
def add_outfit_products
#products = current_user.products
end
def add_outfit_similar_products
#products = current_user.products
end
private
def create_product_images
params["product"]["product_images_attributes"].each do |index, image|
ProductImage.create(product_image: image, product_id: #form.product.id)
end
end
def set_product
#product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(
:title,
:price,
:description,
:tag_list,
:category_id,
:size_description,
:shipping_description,
product_images_attributes: [:product_image, :_destroy],
product_sizes_attributes: [:size_id, :quantity]
)
end
def correct_user_edit
if #product = current_user.products.find_by(id: params[:id])
else
redirect_to root_url if #product.nil?
end
end
end
it seems like your #categories = nil, but should be #categories = [] array.
Check your Controller - Action edit, do you define #categories?

How to flash error for particular validation

I am trying to flash messages in a form when they do not meet the validation requirements but can't work out how to achieve this.
I have the following setup:
models/item.rb
class Item < ActiveRecord::Base
attr_accessible :condition, :day, :description, :subtitle, :title
validates :user_id, presence: true
validates :title, presence: true
validates :description, presence: true, length: { minimum: 20 }
belongs_to :user
end
controllers/items_controller.rb
class ItemsController < ApplicationController
before_filter :authenticate_user!
def new
#item = Item.new
end
def show
#item = Item.find(params[:id])
end
def create
#item = current_user.items.build(params[:item])
if #item.save
flash[:success] = "Your item has been saved"
redirect_to root_path
else
render 'new'
end
end
def destroy
#item.destroy
redirect_back_or root_path
end
end
and finally views/items/new.html.erb
<h1>Items Base</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#item) do |f| %>
<%= f.label :title, "Title" %>
<%= f.text_field :title %>
<%= f.label :subtitle, "Subtitle" %>
<%= f.text_field :subtitle %>
<%= f.label :condition, "Condition" %>
<%= f.number_field :condition %>
<%= f.label :description, "Description" %>
<%= f.text_field :description %>
<%= f.label :day, "Day" %>
<%= f.text_field :day %>
<%= f.submit "List", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Essentially I would like to be able to flash the message "Description too short!" when the user leaves it blank or below 20 characters, or flash the message "Title required" if it is left blank. Any thoughts on how best to achieve this. Also if anyone has any good resources on working with the flash it would be much appreciated. Thanks.
Here is how I do it:
Here is the form:
<%= form_for(#client) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
And here is my error_messages partial:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

nil is not a symbol when uploading file using Carrierwave

i have searched a lot.
I'm facing this error
Create method in Controller is
def create
#category = Category.new(params[:category])
respond_to do |format|
if #category.save
format.html { redirect_to #category, notice: 'Category was successfully created.' }
format.json { render json: #category, status: :created, location: #category }
else
format.html { render action: "new" }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
My Form is
<%= form_for #category, :html => { :multipart => true } do |f| %>
<% if #category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#category.errors.count, "error") %> prohibited this category from being saved:</h2>
<ul>
<% #category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description%>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When i select the file to upload and click submit button not proceed
My Uploader is
# encoding: utf-8
require 'carrierwave/processing/rmagick'
class ImageUploader < CarrierWave::Uploader::Base
#Include RMagick or MiniMagick support
include CarrierWave::RMagick
# Choose what kind of storage to use for this uploader:
storage :file
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def scale(width, height)
# do something
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [80, 80]
end
end
i have Create the new project, apply everything in the same way and its working, still don't know why this error occurred but now i have no problem.

Can't trigger this controller action in a view (it isn't a default Rails RESTful action)

I have a controller called votes_controller.rb. In that file there is the following action:
class VotesController < ApplicationController
def vote_up
#post = Post.find(params[:post_id])
vote_attr = params[:vote].merge :user_id => current_user.id, :polarity => 1
#vote = #post.votes.create(vote_attr)
end
(etc...)
I want to trigger the vote_up action in a view:
views/posts/show.html.erb::
<%= link_to "Vote Up", ??? %>
Here is the whole file just in case:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<%= link_to "Vote Up", ??? %>
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I have no idea what to type in the ??? part (I would also like to make it work as :remote. My intention is to trigger the action without refreshing the page).
Do I have to add something in the routes.rb?
Any suggestions?
You have to define a route in routes.rb. Use a named route to be easy to use in the view. Something like:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
And so can now use in the view
<%= link_to "Vote Up", vote_up_path(#post) %>
and in the controller
def vote_up
#post = Post.find(params[:id])
...
end
See Rails routing

Rails 3 Routes issue on form Edit

Basically whats happening is I can create a new item that gets saved to my table in my db. but when I go to edit the item, the form opens up, I make the change and then when I go to submit, it takes me to the same url as the edit page and gives me Routing Error No route matches "/support/14/edit" although if you enter that in the address bar it opens the edit form just fine, but doesn't have any of my changes saved. So here is my code.
routes.rb
resources :support
support_controller.rb
def new
#support_item = Support.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #support_item }
end
end
# GET /support/1/edit
def edit
#support_item = Support.find(params[:id])
end
# POST /support
# POST /support.xml
def create
#support_item = Support.new(params[:support_item])
respond_to do |format|
if #support_item.save
format.html { redirect_to("/support", :notice => 'Question was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
# PUT /support/1
# PUT /support/1.xml
def update
#support_item = Support.find(params[:id])
respond_to do |format|
if #support_item.update_attributes(params[:support_item])
format.html { redirect_to("/", :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #support_item.errors, :status => :unprocessable_entity }
end
end
end
support.rb
class Support < ActiveRecord::Base
belongs_to :role
scope :admin_available, order("role_id ASC") do
Support.all
end
def self.available(user)
questions = where(:role_id => 1)
questions += where(:role_id => user.roles)
questions
end
end
_form.html.erb
<% if #support_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#support_item.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #support_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Support item for:" %><br />
<%= f.collection_select :role_id, Role.find_by_max(5), :id, :name, {:default => 'everyone'} %>
</div>
<div class="field">
<%= f.label :question %><br />
<%= f.text_field :question, :class => 'genForm_question'%>
</div>
<div class="field">
<%= f.label :answer %><br />
<%= f.text_area :answer, :class => 'genForm_textarea' %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url, :class => 'genForm_question' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
new.html.erb
<h1>New Support Item</h1>
<% form_for #support_item, :url => { :action => "create" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
edit.html.erb
<h1>Editing Support Item</h1>
<% form_for #support_item, :url => { :action => "edit" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
I believe thats all the relavent code.
<h1>Editing Support Item</h1>
<% form_for #support_item do |f| %>
<%= render 'form', :f => f %>
<% end %>
You are overriding the URL. It should be able to be auto-generated like that if you are doing everything with standard rest. If that doesn't work out, just know you don't want to submit to /support_items/1/edit, you want to submit to /support_items/1.