0
我无法加载表单,不知道为什么会发生这种情况。表单在Rails 3中没有正确加载
问题:当我尝试加载表单(/ messages/new)时,我得到一个空白页面,没有错误信息。
理由对这个问题:我认为问题是,消息和用户之间的联系没有被正确地建立起来,这就是为什么形式不正确加载,但不知道如何解决它。
背景:我目前通过POSTS/INDEX文件(附在下面)访问表单,但我认为这是错误的,因为每个用户应该有一个独特的链接......例如,如果我点击John创建的帖子的“联系人”按钮,则该消息应该发送给John,以此类推其他用户。我正在使用Simple Private Messaging插件。
感谢您的任何反馈!
感谢,
费萨尔
信息>新VIEW
<% form_for @message, :url => messages_path(:user_id => @user) do |f| %>
<p>
To:<br />
<%= f.text_field :to %>
<%= error_message_on @message, :to %>
</p>
<p>
Subject:<br />
<%= f.text_field :subject %>
<%= error_message_on @message, :subject %>
</p>
<p>
Message<br />
<%= f.text_area :body %>
<%= error_message_on @message, :body %>
</p>
<p>
<%= submit_tag "Send" %>
</p>
<% end %>
消息模型
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
的routes.rb
Mysalary::Application.routes.draw do
resources :messages do
collection do
post :delete_selected
end
end
resources :users
resources :profiles
resources :pages
resources :posts
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
MESSAGES CONTROLLER
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
@messages = @user.sent_messages
else
@messages = @user.received_messages
end
end
def show
@message = Message.read_message(params[:id], current_user)
end
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
def delete_selected
if request.post?
if params[:delete]
params[:delete].each { |id|
@message = Message.find(:first, :conditions => ["messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, @user, @user])
@message.mark_deleted(@user) unless @message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to :back
end
end
private
def set_user
@user = User.first
end
end
帖子> INDEX VIEW
<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>
感谢来源!!!它的工作原理..但由于某些原因,如果我访问/消息/新的独立于任何用户(即直接),页面仍然打开..如何禁用? – hikmatyar 2012-04-21 18:02:19
在http://api.rubyonrails.org中输入'ActionController :: Redirecting'获取一些文件,说明如果你不想让用户访问它,那么你需要做些什么来重定向用户, – lcarpenter 2012-04-21 20:27:08