mercredi 8 juin 2016

Error : ArgumentError in Messages#new wrong number of arguments (2 for 1)

I m getting ArgumentError in Messages#new app/views/messages/new.html.erb where line #16 raised: "wrong number of arguments (2 for 1)" It also says:

app/helpers/application_helper.rb:8:in avatar_url' app/helpers/messages_helper.rb:5:inblock in recipients_options' app/helpers/messages_helper.rb:4:in recipients_options' app/views/messages/new.html.erb:16:inblock in _app_views_messages_new_html_erb___264831154_80406348' app/views/messages/new.html.erb:3:in `_app_views_messages_new_html_erb___264831154_80406348'

My application helper:

module ApplicationHelper

def page_header(text)
    content_for(:page_header) { text.to_s }
end


def avatar_url(user)
    if user.image
        user.image
    else
        "/images/missing.png"
    end
end
end

My messages helper is,

module MessagesHelper


def recipients_options(chosen_recipient = nil)
    s = ''
    User.all.each do |user|
      s << "<option value='#{user.id}' data-img-src='#{image_tag avatar_url(user.email, size: 50)}' #{'selected' if user == chosen_recipient}>#{user.name}</option>"
    end
    s.html_safe
  end
end

my messages/new.html.erb is,

<% page_header "Start Conversation" %>

<%= form_tag messages_path, method: :post do %>
  <div class="form-group">
    <%= label_tag 'message[subject]', 'Subject' %>
    <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'message[body]', 'Message' %>
    <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
    <%= label_tag 'recipients', 'Choose recipients' %>
    <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
  </div>

  <%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>

messages_controller.rb

class MessagesController < ApplicationController


 before_action :authenticate_user!

  def new
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
  end

  def create
    recipients = User.where(id: params['recipients'])
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
    flash[:success] = "Message has been sent!"
    redirect_to conversation_path(conversation)
  end
end

Aucun commentaire:

Enregistrer un commentaire