methyl
methyl

Reputation: 3312

Rails creating reply for message

Is it right approach to create a form with hidden fields like this:

#show.html.haml
= semantic_form_for @reply do |f|
      = f.hidden_field :sender, :value => @message.sender_id
      = f.hidden_field :receiver, :value => @message.receiver_id
      = f.hidden_field :title, :value => @message.reply_title
      = f.input :content, :as => :text
      = f.submit

#private_messages_controller.rb
  def show
    @message = PrivateMessage.find(params[:id])
    @message.read! if @message.receiver == current_user
    @reply = PrivateMessage.new
  end

  def new
    @message = PrivateMessage.new
  end

  def create
    @message = PrivateMessage.new params[:private_message]
    @message.sender = current_user
    if @message.save
      redirect_to user_path(@message.receiver), :notice => "Wiadomość została wysłana"
    else
      render :new
    end
  end

to make a reply for a private message, or there is a better way to do it?

Upvotes: 0

Views: 365

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96504

I have learned from experience that hidden fields are usually a sign of an incorrect approach.

In this case, for sender I would look at scope, in this case using the relationship and then constructs like current_user.message

I would make message_Type (values private and public) a variable of flag, not a different Model.

So my approach would probably be to get the models right first and then build on them.

Upvotes: 1

Cydonia7
Cydonia7

Reputation: 3826

Of course, this is the right approach because all the variables you use as hidden field are linked to a specific form and not to some kind of session so this is the good way to store these I think

Upvotes: 1

Related Questions