Rails - 创建模型时出现问题
我使用的模型属于另外 2 个模型。当我尝试创建它时,我设法获取两个 id,但内容本身并未存储在数据库中
def create
@person = Person.find(current_person)
@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person))
if @message.save
redirect_to(:back)
else
redirect_to(:back)
end
end
<% form_for(:message, :url => messages_path(:person_id => current_person.id, :group_id => @group.id)) do |f| %>
<%= f.text_area :content %>
<%= f.submit "Submit" %>
<%end %>
此外,content
在数据库中设置为文本,并且我正在使用 PostgreSQL。
I'm using a model that belongs to 2 other models. When I try to create it, I manage to get both ids, but the content itself isn't stored in database
def create
@person = Person.find(current_person)
@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person))
if @message.save
redirect_to(:back)
else
redirect_to(:back)
end
end
<% form_for(:message, :url => messages_path(:person_id => current_person.id, :group_id => @group.id)) do |f| %>
<%= f.text_area :content %>
<%= f.submit "Submit" %>
<%end %>
Also, content
is set as text in database and I'm using PostgreSQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@why 上面的答案应该适合你。但你可以更进一步,利用联想的力量。
在您的 message.rb 中,您将具有关联,
您也可以在声明 has_many 关系的 Group / Person 模型中具有类似的关联。
In routes.rb (Rails 2.3.x)
In routes.rb (Rails 3)
这将为您提供一条类似于
You are using current_person 的路线,该路线似乎与当前登录相关,因此通过 url 或参数使其可见或可编辑并不是一个好主意。 current_person 应该从创建操作本身的会话中派生。
@why's answer above should do it for you. But you can go a step above and use the power of associations.
In your message.rb, you would have the association
You could also have a similar association in Group / Person models which declares a has_many relationship.
In routes.rb (Rails 2.3.x)
In routes.rb (Rails 3)
This will give you a route like
You are using current_person which seems to be current login related, so it would not be a good idea to make it visible or editable thru the url or parameters. current_person should be derived from the session in the create action itself.
尝试更改
为
@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person), :content => params[:content])
Try changing
to
@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person), :content => params[:content]
)