Rails:错误参数丢失或值为空:annonce

时间:2015-05-16 13:49:55

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 active-form

我想为学校的项目创建一个分类广告网站,我尝试创建一个表单,通过电子邮件向网站成员发送消息。电子邮件地址包含在名为“Membre”的模型中,此模型是名为“Annonce”的模型的链接,其中包含广告。

但是当我尝试创建它时,我有这个错误:

  

param丢失或值为空:annonce

     

app / controllers / annonces_controller.rb:104:在`annonce_params'

     

app / controllers / annonces_controller.rb:29:在'create'

Ad控制器:

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

我该如何解决?

提前致谢

1 个答案:

答案 0 :(得分:0)

这是路由错误,您应该调用create的{​​{1}}方法,而不是FormContactsController方法。

AnnoncesController create