1

I want to be able to access :to_whom text value via params[:to_whom] in the controller. To_whom does not exist in a model.

I get the sensible error: 'undefined method `to_whom' for Conversation'

How can I add an arbitrary attribute to pass back to the controller in rails?

Also, in my view I did Message.new and Conversation.new which is incredibly ugly. I initially set @conversation = Conversation.new in the controller, however I found I had to recreate those variables in the second controller method anyways, which makes sense (after I hit the submit button). Thus instead of setting @message, @conversation in the new method, I removed all the lines from new and did the .new syntax in the view. Is there a more elegant way of writing this code so it isn't so hacky feeling?

CONTROLLER:

 class ConversationsController < ApplicationController
   attr_accessor :conversation, :user, :to_whom

   # this is the method that generates the below view
   def new
   end

   def create
      ...
   end
 end

VIEW:

<%= form_for([current_user, Conversation.new]) do |c| %>
  <%= c.label :to_whom %>
  <%= c.text_field :to_whom %>

  <%= c.label :subject %>
  <%= c.text_field :subject %>   
  <%= form_for(Message.new) do |m| %>
  <%= m.label :message %>
  <%= m.text_field :text %>

    <div class="actions">
      <%= submit_tag "send" %>
    </div>
  <% end %>
<% end %>
zishe
  • 10,665
  • 12
  • 64
  • 103
LLL
  • 1,085
  • 8
  • 16

3 Answers3

2

m.text_field :to_whom is just a helper to build an html input tag. You could write your own in raw html, filling in the blanks with erb tags, or you could use other helpers, such as text_field_tag:

text_field_tag "to_whom", params[:to_whom]
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • thanks, this is exactly what I was looking for. I didn't see this in the docs :D I will approve your answer. You answered really quick so StackOverFlow isn't letting me do it yet. – LLL Jun 03 '14 at 08:18
  • Did it work? I thought the issue would be with your declaration of `attr_accessor` – Richard Peck Jun 03 '14 at 08:21
  • Actually that's a good point - i missed the attr_accessor. The attr_accessor doesn't work because you've defined it in the controller. It needs to be in the Conversation model. – Max Williams Jun 03 '14 at 08:22
  • You should give @RichPeck the right answer, mine is more like a work around for you not using attr_accessor properly. – Max Williams Jun 03 '14 at 08:23
2

Virtual Attributes

Your attr_accessor belongs in your model (not your controller). Currently, you have it stored in your controller, which will do nothing at the model-level:

#app/models/conversation.rb
Class Conversation < ActiveRecord::Base
    attr_accessor :conversation, :user, :to_whom
end

#app/controllers/conversations_controller.rb
    # remove attr_accessor 

You have to remember that since Ruby is object-orientated, all the data objects you get are from the model. This means if you call @conversation = Conversation.new, the attributes of that model are actually created in the conversation model

Normally, the model will set the attributes in accordance with your database columns. If you don't have a database column present, you need to create the relevant getter / setter methods using the attr_accessor module

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

I think you need to use fields_for helper, It should be something like below,

= c.fields_for :message do
Mohanraj
  • 4,056
  • 2
  • 22
  • 24