从静态页面渲染部分内容 [英] Render partial from static page

查看:216
本文介绍了从静态页面渲染部分内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的主要欢迎页面中呈现部分内容(我使用HighVoltage gem来创建它), 我的网上论坛表单,用户可以在其中创建新的网上论坛...

I'm trying to render a partial in my main welcome page (i used HighVoltage gem to create it), my Groups form, where the user can create a new group...

到目前为止我尝试过的东西给了我以下错误...

what i've tried so far gives me the following error...

表单中的第一个参数不能包含nil或为空

= form_for @group do |f| 
    .fieldset
      - if @group.errors.any?
        .error_messages

我的路线是这样配置的

Giraffle::Application.routes.draw do
    get 'sign_up', to: 'groups#new', as: 'sign_up'
    get 'sign_in', to: 'sessions#new', as: 'sign_in'
    delete 'sign_out', to: 'sessions#destroy', as: 'sign_out'

    resources :sessions
    resources :members
  resources :groups
  resources :events
  resources :event_sets

  root :to => 'high_voltage/pages#show', id: 'welcome'
end

因此,当我尝试加载主页时,直接转到欢迎页面,并由于上述错误而崩溃

So when i try and load the main page goes straight to the welcome page and it crashes by the error above

我想我知道问题出在哪里...但是我不知道该如何解决.我不尝试呈现表单,但是由于组" viarable从未初始化,因此会引发此错误.

i think i know what the problem is... but i don't knwo how to solve it. I'ts trying to render the form, but since the "group" viarable is never initialize it throws this error.

我的代码...

views/pages/welcome.html.slim

views/pages/welcome.html.slim

row id="div"
  .small-4 id="innerDiv"
  .row
    .small-4.columns align="center"
      img src="groupIcon.png" id="mainImg" 
    .large-6.large-offset-2.columns
      h1 Sign Up
      = render :partial => '/groups/form'

views/groups/_form.html.slim

views/groups/_form.html.slim

= form_for @group do |f| 
  .fieldset
    - if @group.errors.any?
      .error_messages
        h2 Form is invalid
        ul
          - @group.errors.full_messages.each do |message|
            li= message
    .row
      .small-12.columns
        = f.text_field :name, placeholder: "Name"
    .row
      .small-12.columns
        = f.text_field :group_id, placeholder: "Group"
    .row
      .small-12.columns
        = f.password_field :password, placeholder: "Password"
    .row
      .small-12.columns
        = f.password_field :password_confirmation, placeholder: "Confirm Password"
    .row
      .small-3.columns
        .actions= f.submit 'Sign Up', class: 'button radius'

编辑

controllers/groups_controller.rb

controllers/groups_controller.rb

class GroupsController < ApplicationController
    load_and_authorize_resource

    before_action :set_group, only:     [:edit, :update, :destroy]
    before_action :authorize, except: [:new,  :create]

    def new
        @group = Group.new
    end

    def edit
    end

    def create
        @group = Group.new(group_params)

        if @group.save
            redirect_to root_url, notice: 'Signed Up!' 
        else
            render 'new'
        end
    end

    def update
        if @group.update(group_params)
      redirect_to root_url, notice: 'Group Info was successfully updated.'
    else
      render action: 'edit'
    end
  end

    private
        def set_group
      @group = Group.find(params[:id])
    end

        def group_params
            params.require(:group).permit(:group_id, :name, :password, :password_confirmation)
        end
end

推荐答案

我设置了一个高压演示应用程序,该应用程序带有用于创建查询"的表格.

I set up a High Voltage demo application with a form for creating an "Inquiry."

此处是所做的更改,以使表单正常工作.

以下是高级步骤:

创建一个将在视图中使用的表单:

Create a form that will be used in the view:

<%= simple_form_for resource, html: { novalidate: true }, remote: true do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :comments, as: :text %>
  <%= f.button :submit, value: 'Submit' %>
<% end %>

创建一个控制器来处理请求:

Create a controller to process the request:

class InquiriesController < ApplicationController
  def create
    @inquiry = Inquiry.new(params[:inquiry])
    @inquiry.save
  end
end

Inquiry对象上的.save方法将神奇地通过电子邮件发送请求或将其保存到数据库等.请注意,对valid?的调用是由ActiveModel::Model提供的,如果返回,则返回true.所有的验证都通过了.

The .save method on the Inquiry object will do the magic of emailing the request, or saving to a database, etc. Notice the call to valid? this is provided by ActiveModel::Model and will return true if all the validations pass.

class Inquiry
  include ActiveModel::Model

  attr_accessor :comments, :email, :name

  validates :comments, presence: true
  validates :email, presence: true, email: true
  validates :name, presence: true

  def save
    if valid?
      # send an email or persist to the database...
    end
  end
end

通过使用.js.erb文件,我们可以从控制器端点向静态页面返回成功消息或错误.

By using .js.erb files we can return a success message, or errors to the static page from the controller endpoint.

<% if @inquiry.valid? %>
  $('#inquiry_form').html('<h2>Thank you for contacting us!</h2>');
<% else %>
  $('#inquiry_form').html('<%= j render 'inquiry_form', resource: @inquiry %>');
<% end %>

此操作是在请求有效时返回成功"消息,而在请求无效时返回带有错误的表单.

What this does is return the "success" message if the request is valid, and returns the form with errors if the request is invalid.

这篇关于从静态页面渲染部分内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆