0

Я хочу создать сайт для объявлений о проекте в школе, и я пытаюсь создать форму для отправки сообщения по электронной почте на веб-сайте. Адрес электронной почты содержится в модели, которая называется «Membre», и эта модель является ссылкой на модель, которая является именем «Annonce», в котором содержится объявление.Rails: ошибка param отсутствует или значение пусто: annonce

Но когда я пытаюсь создать что, у меня есть эта ошибка:

пары отсутствуют или значение пусто: АНОНС

приложения/контроллеры/annonces_controller.rb: 104: в `annonce_params '

приложение/контроллеры/annonces_controller.rb: 29: в `создать'

Здесь контроллер объявления:

class AnnoncesController < ApplicationController 

    before_action :set_annonce, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, :except => [:index] 

    # GET /annonces 
    # GET /annonces.json 
    def index 
    @annonces = Annonce.all 
    end 

    # GET /annonces/1 
    # GET /annonces/1.json 
    def show 
    end 

    # GET /annonces/new 
    def new 
    @annonce = Annonce.new 
    end 

    # GET /annonces/1/edit 
    def edit 
    end 

    # POST /annonces 
    # POST /annonces.json 
    def create 
    @annonce = Annonce.new(annonce_params) 
    @annonce.membre_id = current_membre.id 
    respond_to do |format| 
     if @annonce.save 
     format.html { redirect_to @annonce, notice: t('annonce_cree_succes') } 
     format.json { render :show, status: :created, location: @annonce } 
     else 
     format.html { render :new } 
     format.json { render json: @annonce.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /annonces/1 
    # PATCH/PUT /annonces/1.json 
    def update 
    respond_to do |format| 
     if @annonce.update(annonce_params) 
     format.html { redirect_to @annonce, notice: t('annonce_cree_succes') } 
     format.json { render :show, status: :ok, location: @annonce } 
     else 
     format.html { render :edit } 
     format.json { render json: @annonce.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /annonces/1 
    # DELETE /annonces/1.json 
    def destroy 
    @annonce.destroy 
    respond_to do |format| 
     format.html { redirect_to annonces_url, notice: t('annonce_destroy_succes') } 
     format.json { head :no_content } 
    end 
    end 

    # GET /annonces/contact/1 
    def contact 
    @form_contact = FormContact.new 
    if @form_contact.valid? 
     #MembreMailer.email_contact(Membre.where(:id => @annonce.membre_id),current_membre,@annonce,@message) 
     @annonce = Annonce.find(params[:id]) 
     @recepteur = Membre.where(:id => @annonce.membre_id) 
     @membre = current_membre 
     mail(:to => "#{recepteur.pseudo} <#{recepteur.email}>", subject: 'Reponse à l\'une de vos annnonces') 
     redirect_to root 
    end 
    end 

    # GET /annonces/report/1 
    def report 
    @annonce = Annonce.find(params[:id]) 
    end 

    private 

    def authenticate_user! 
     if membre_signed_in? 
      #super 
     else 
      redirect_to login_path, :notice => 'Merci de vous connecter pour effecter cette action' 
      ## if you want render 404 page 
      ## render :file => File.join(Rails.root, 'public/404'), :formats => [:html], :status => 404, :layout => false 
     end 
    end 
# Use callbacks to share common setup or constraints between actions. 
def set_annonce 
    @annonce = Annonce.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def annonce_params 
    params.require(:annonce).permit(:id, :titre, :corps, :prix, :membre_id, :categorie, :created_at, :image) 
end 

Здесь вид контакта:

<div class="col-md-offset-2 col-md-8 well panel panel-default"> 
    <h2 class='panel-heading text-center'><%= t('contacter') %></h2> 
    <div class="panel-body text-center"> 
     <%= form_for(:form_contact, :url => {:action => :create}) do |f| %> 
      <div class="field block-center"> 
       <%= f.label "message" %></br> 
       <%= f.text_area(:message, size: "50x15")%> 
      </div></br> 
      <div class="actions form-group col-md-offset-3 col-md-6"> 
       <%= submit_tag t('envoyer'), :class => "btn btn-large btn-block btn-primary" %> 
      </div> 
     <% end %> 
    </div> 
</div> 

<p class='row'> </p> 

А вот FormContact класс:

class FormContact < ActiveForm::Base 
    attr_accessor :message 
    validates_presence_of :message 

    def new 
     @form_contact = FormContact.new(login_form) 
    end 

    def index 
     @form_contact = FormContact.new 
    end 

    private 

    def login_form 
     params.require(:form_contact).permit(:message) 
    end 
end 

Как я могу исправить это?

Заранее спасибо

+0

Подумайте, что вы ответили, но я забыл о конце файла контроллера Annonce в своем сообщении ... Я проверил содержимое параметров и проблем нет. Я забыл написать ошибку, когда я отправлю сообщение – Sraye25

+0

, можете ли вы показать нам контент запроса? – pangpang

+0

Запрос: { "utf8" => "✓", => "2yv/ф + 2KXPsa8gavyxMAefbcVevfY7imXJmD7j + s3NiLy09w1/FOAzu4PYqVynYOT4CLbm2AYHnXr5HqSZd5Q ==", "form_contact" => { "сообщение" "authenticity_token" => "Это message "}, " commit "=>" Envoyer ", " locale "=>" fr "} – Sraye25

ответ

0

Это маршрутизация ошибки, вы должны вызвать метод create из FormContactsController, не AnnoncesController create метода.

Смежные вопросы