0

Rails でプログラミングをするのは久しぶりです... Rails 5.0 のすべての構文と変更点について最新情報を取得しています。

Rails 5.0.0.1 の使用

Ruby を使用する ruby​​ 2.3.1p112 (2016-04-26 リビジョン 54768) [x86_64-darwin16]

ランディングページに簡単なお問い合わせフォームを設定しようとしています。フォームから直接メールを送信するのではなく、データベースに保存するというルートをたどっています。

私はmail_form gem を使用しており、このスレッドに従っています

コントローラーで初歩的なミスを犯していることはわかっていますが、いくつかのスタック Q/A を行った後でも、まだ完全には達成できていません。

モデルは Rails コンソールで正常に電子メールを送信しています。ただコントローラーが動かない。これは 1 ページのサイトなので、Pages View フォルダの Index ページにパーシャルを追加しています。

私が得ているエラー

AbstractController::ActionNotFound (The action 'create' could not be found for PagesController):

ルート

 Rails.application.routes.draw do
  get 'users/new'
  resources :pages
  root 'pages#index'
end

部分的なフォーム

アプリ/ビュー/ページ/_form.html.erb

<%= form_tag(pages_path)  do %>
    <div class="row">
        <div class="column width-6">
            <%= text_field_tag 'firstname', nil, class: 'form-element rounded large', placeholder: 'First Name*', tabindex: '1' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'lastname', nil, class: 'form-element rounded large', placeholder: 'Last Name*', tabindex: '2' %>
        </div>
        <div class="column width-6">
            <%= email_field_tag 'email', nil, class: 'form-element rounded large', placeholder: 'Email Address*', tabindex: '3' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'website', nil, class: 'form-element rounded large', placeholder: 'Website', tabindex: '4' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'phone', nil, class: 'form-element rounded large', placeholder: 'Phone', tabindex: '5' %>
        </div>
    </div>
    <div class="row">
        <div class="column width-12">
            <%= text_area_tag 'message', nil, class: 'form-element rounded large', placeholder: 'Message*', tabindex: '6' %>
        </div>
        <div class="column width-12">
            <%= submit_tag 'Send Email', class: 'form-submit button rounded medium bkg-theme bkg-hover-green color-white color-hover-white'  %>
        </div>
    </div>
<% end %>

ページコントローラー

class PagesController < ApplicationController
  def index
    @contact = Page.new(params[:page])
    if @contact.deliver
      redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
    else
      flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
    end
  end
end

助けてくれてありがとう。このコミュニティは素晴らしいです!

4

2 に答える 2

0

redirect_to :backは Rails 5 で廃止されました。代わりに、 という新しい関数がありますredirect_back

ただしindex、データベースに保存しない場合でも、新しいページを作成するためにアクションを使用することはありません。代わりに、呼び出される新しいアクションを定義し、最後createにリダイレクトしindexます。すでにresources :pagesルートで使用しているため、そこに何も追加する必要はありません。ここでは、デフォルトのルートとそのアクション、およびそれらの使用目的を確認できます: http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

また、モデルを使用している場合は、form_for代わりに使用することを検討します。form_tagここに簡単な例があります: http://edgeguides.rubyonrails.org/getting_started.html#the-first-form

これが少し役に立てば幸いです:)

于 2016-09-27T14:24:36.617 に答える
0

ページコントローラーのルートがありません。

加えてconfig/routes.rb

resources :pages

PagesController.rbで

class PagesController < ApplicationController
  def create
    @contact = Page.new(params[:page])
    if @contact.deliver
      redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
    else
      flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
    end
  end
end

AJAX を処理しますpost

于 2016-09-27T02:44:23.220 に答える