Reputation: 247
On my homepage there is an account creation (users) form, a submission form for designs (designs), and I'm trying to add a section on the same page where users can view active designs and vote on them. I'm having problems adding that section to the page because of the submission form I think - here are the relevant files:
landing-controller (homepage controller)
class LandingController < ApplicationController
def index
@email = Email.new
@design = Design.all
@user = User.new
end
end
Design.new
it works for the submission and not the view.designs-controller
class DesignsController < ApplicationController
protect_from_forgery with: :exception
before_filter :admin_user, only: [:show ,:edit, :update, :destroy]
def show
@design = Design.find(params[:id])
end
def edit
@design = Design.find(params[:id])
end
def new
@design = Design.new
end
def create
@design = Design.new(design_params)
respond_to do |format|
if @design.save
format.html { redirect_to root_path, notice: 'Thank You For Your Submission!' }
format.json { render json: Design.create(design_params) }
else
@user = User.new
format.html { render "landing/index" }
format.json { render :json => @design.errors, :status => :unprocessable_entity }
end
end
end
def update
@design = Design.find(params[:id])
if @design.update(design_params)
redirect_to '/cpanel'
else
render 'edit'
end
end
def destroy
@design = Design.find(params[:id])
@design.destroy
redirect_to '/cpanel'
end
private
def design_params
params.require(:design).permit(:dfirstname, :dlastname, :demail, :rcode, :frontview, :sideview, :backview, :category, :active)
end
def admin_user
redirect_to(root_path) unless current_user && current_user.admin?
end
end
Then I have this in the landing view:
<% @design.each do |design| %>
<% if design.active %>
<%= image_tag design.frontview, :class => 'design-image' %>
<% else %>
<% end %>
<% end %>
and this below it
<%= form_for @design.new, url: designs_path, html: {class: "design-form", :multipart => true} do |f| %>
<h2>Design Submission</h2>
<% if @design.new.errors.any? %>
<ul>
<% @design.errors.full_messages.each do |message| %>
<li class="e-message">- <%= message %></li>
<% end %>
</ul>
<% end %>
... form ...
<% end %>
Basically getting these to work together, as of right now it will load the page, but then give me an error message when I try to submit the form. The specific error message is undefined method each
in the landing index page.
Upvotes: 1
Views: 2340
Reputation: 33542
You can't use @design
in the form
as you defined as @design = Design.all
. You should change @design = Design.all
to @designs = Design.all
and add @design = Design.new
in the index method
class LandingController < ApplicationController
def index
@email = Email.new
@designs = Design.all
@design = Design.new
@user = User.new
end
end
And also change
<% @design.each do |design| %>
to
<% @designs.each do |design| %>
and
<%= form_for @design.new, url: designs_path, html: {class: "design-form", :multipart => true} do |f| %>
to
<%= form_for @design, url: designs_path, html: {class: "design-form", :multipart => true} do |f| %>
in the view page.
Update:
As per our discussion and the code of the design_controller.rb
, in the create
method in the else
block you should have @designs = Design.all
because if the creation is failed you are rendering index.html.erb
with this code format.html { render "landing/index" }
, so Rails doesn't know where @designs
come from in this case.
def create
@design = Design.new(design_params)
respond_to do |format|
if @design.save
format.html { redirect_to root_path, notice: 'Thank You For Your Submission!' }
format.json { render json: Design.create(design_params) }
else
@user = User.new
@designs = Design.all
format.html { render "landing/index" }
format.json { render :json => @design.errors, :status => :unprocessable_entity }
end
end
end
Upvotes: 2