Ruby on Rails Form&在同一页面上查看

时间:2015-09-29 13:16:55

标签: html ruby-on-rails ruby forms

在我的主页上有一个帐户创建(用户)表单,一个设计(设计)的提交表单,我正在尝试在同一页面上添加一个部分,用户可以在其中查看活动设计并对其进行投票。由于我认为提交表单,我在将该部分添加到页面时遇到问题 - 这里是相关文件:

登陆控制器(主页控制器)

class LandingController < ApplicationController

    def index
        @email = Email.new
        @design = Design.all
        @user = User.new
    end

end
  • 当我将@design更改为Design.new时,它适用于提交而不是视图。

设计控制器

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

然后我在着陆视图中有这个:

<% @design.each do |design| %> 
    <% if design.active %>
        <%= image_tag design.frontview, :class => 'design-image' %>
    <% else %>

    <% end %>
<% end %>

以及它下面的

<%= 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 %>

基本上让它们一起工作,现在它将加载页面,但在我尝试提交表单时给我一条错误消息。着陆索引页中的特定错误消息为undefined method each

1 个答案:

答案 0 :(得分:2)

您无法在@design中使用form,因为您定义为@design = Design.all。您应该将@design = Design.all更改为@designs = Design.all并在索引方法中添加@design = Design.new

class LandingController < ApplicationController

  def index
    @email = Email.new
    @designs = Design.all
    @design = Design.new
    @user = User.new
  end
end

并且还要改变

<% @design.each do |design| %>

<% @designs.each do |design| %>

<%= form_for @design.new, url: designs_path, html: {class: "design-form", :multipart => true} do |f| %>

<%= form_for @design, url: designs_path, html: {class: "design-form", :multipart => true} do |f| %>

在视图页面中。

<强> 更新

根据我们的讨论和design_controller.rb的代码,在create块的else方法中,您应该@designs = Design.all,因为如果创建失败,您将获得index.html.erb使用此代码format.html { render "landing/index" }呈现@designs,因此在这种情况下,Rails不知道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 来自何处。

Q