我收到以下錯誤,當我嘗試提交表單(形式顯示出來時,1個用戶想要消息的另一個用戶)未定義的方法`stringify_keys'的消息控制器
NoMethodError in MessagesController#create
undefined method `stringify_keys' for "":String
Rails.root: /Users/fkhalid2008/loand
Application Trace | Framework Trace | Full Trace
app/controllers/messages_controller.rb:31:in `new'
app/controllers/messages_controller.rb:31:in `create'
下面是相關代碼來自消息控制器:
def new
@message = Message.new
if params[:reply_to]
@reply_to = @user.received_messages.find(params[:reply_to])
unless @reply_to.nil?
@message.to = @reply_to.sender.login
@message.subject = "Re: #{@reply_to.subject}"
@message.body = "\n\n*Original message*\n\n #{@reply_to.body}"
end
end
end
def create
@message = Message.new(params[:message])
@message.sender = @user
@message.recipient = User.find_by_login(params[:message][:to])
if @message.save
flash[:notice] = "Message sent"
redirect_to user_messages_path(@user)
else
render :action => :new
end
end
我該如何解決這個問題?
更多個支持代碼如下:
MESSAGES>(這是其中正在創建表)NEW VIEW
<%= form_for @message, :url => messages_path(:user_id => @user) do |f| %>
<br>
<br />
<br />
<div class="field">
<label>Name</label>
<input type="text" name="name" id="contact_name" class="required" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="email" id="contact_email" class="required" />
</div>
<div class="field">
<label>Subject</label>
<input type="text" name="subject" id="contact_subject" class="required" />
</div>
<div class="field">
<label>Message</label>
<textarea name="message" rows="8" cols="25" id="contact_message" class="required max_len", max_length=1000 /></textarea>
</div>
<div class="btn">
<%= submit_tag 'Send Message' %>
</div>
<% end %>
消息模型
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
用戶模型
class User < ActiveRecord::Base
has_many :posts
has_one :profile
has_private_messages
attr_accessible :email
validates_presence_of :email
validates_uniqueness_of :email, :message =>"Hmm, that email's already taken"
validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i, :message => "Hi! Please use a valid email"
end
帖子>索引視圖(這是表格被鏈接到)
<table class="table table-striped">
<tbody>
<% @posts.each do |post| %>
<tr>
<td>I am a <%= post.title %> getting married in <%= post.job %> in <%= post.location %>, and looking for a <%= post.salary %>. My budget is <%= post.salary %>.</td>
<td> <button class="btn" data-toggle="button" onClick="javascript:location.href = '/messages/new';" />Contact</button></td>
<td><%= time_ago_in_words(post.created_at) %> ago.</td>
<!--/.
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
-->
</tr>
<% end %>
</tbody>
</table>
發展LOG
Started POST "/messages" for 127.0.0.1 at Sun Apr 22 01:54:57 +0500 2012
Processing by MessagesController#create as HTML
Parameters: {"name"=>"faisal", "commit"=>"Send Message", "authenticity_token"=>"Qyww9hP6jLmqpQ+ua21FAk9vePju8Gpq39Gpaflf3wE=", "utf8"=>"\342\234\223", "subject"=>"testing", "message"=>"hello", "email"=>"[email protected]"}
User Load (0.1ms) SELECT "users".* FROM "users" LIMIT 1
Completed 500 Internal Server Error in 17ms
NoMethodError (undefined method `stringify_keys' for "hello":String):
app/controllers/messages_controller.rb:31:in `new'
app/controllers/messages_controller.rb:31:in `create'
向我們展示您創建消息的窗體。 '@ user'出現在藍色之外... – jdoe 2012-04-21 20:22:49
@jdoe請參閱上面的內容,我添加了表單和其他支持代碼 – hikmatyar 2012-04-21 20:33:08
您確定您已經發布了正確的數據嗎?該日誌包含完全不同的一組參數。也許這是錯誤的形式?如果你試圖在沒有等號的情況下輸出它,你的表單是如何管理顯示自己的:'