Rails - 创建模型时出现问题

发布于 2024-11-09 10:14:37 字数 625 浏览 0 评论 0原文

我使用的模型属于另外 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-11-16 10:14:37
@message = Message.create params[:message].merge(:group => Group.find(params[:group_id]), :person => Person.find(current_person))
@message = Message.create params[:message].merge(:group => Group.find(params[:group_id]), :person => Person.find(current_person))
暗藏城府 2024-11-16 10:14:37

@why 上面的答案应该适合你。但你可以更进一步,利用联想的力量。

在您的 message.rb 中,您将具有关联,

class Message < ActiveRecord::Base
  ..
  belongs_to :group
  belongs_to :person
  ...
end

您也可以在声明 has_many 关系的 Group / Person 模型中具有类似的关联。

 class Group < ActiveRecord::Base
   ...
   has_many :messages
   ...
 end

 class Person < ActiveRecord::Base
   ...
   has_many :messages
   ...
 end

In routes.rb (Rails 2.3.x)

 map.resources :group, :has_many => [:messages]

In routes.rb (Rails 3)

 resources :groups do
   resources :messages
 end

这将为您提供一条类似于

POST group_messages_path(:group_id) # => this will route to 
                                    # MessagesController's create action
                                    # and provide params[:group_id]

You are using current_person 的路线,该路线似乎与当前登录相关,因此通过 url 或参数使其可见或可编辑并不是一个好主意。 current_person 应该从创建操作本身的会话中派生。

# form_for([@group,:message]) or form_for([@group,@message])
# automatically works out the path as POST /groups/:group_id/messages => MessagesController#create
# And current person association remains safe to currently logged in person
# without being revealed thru parameters and such.

<% form_for([@group,@message]) do |f| %>
  <%= f.text_area :content %> 
  <%= f.submit "Submit" %>
<% end %>


def create
  @group = Group.find(params[:group_id])
  @person = current_person # or however you find current_person
  @message = @group.messages.build(params[:messaage])
  @message.person = @person
  if @message.save
    redirect_to(:back)
  else
    redirect_to(:back)
  end
end

@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

class Message < ActiveRecord::Base
  ..
  belongs_to :group
  belongs_to :person
  ...
end

You could also have a similar association in Group / Person models which declares a has_many relationship.

 class Group < ActiveRecord::Base
   ...
   has_many :messages
   ...
 end

 class Person < ActiveRecord::Base
   ...
   has_many :messages
   ...
 end

In routes.rb (Rails 2.3.x)

 map.resources :group, :has_many => [:messages]

In routes.rb (Rails 3)

 resources :groups do
   resources :messages
 end

This will give you a route like

POST group_messages_path(:group_id) # => this will route to 
                                    # MessagesController's create action
                                    # and provide params[:group_id]

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.

# form_for([@group,:message]) or form_for([@group,@message])
# automatically works out the path as POST /groups/:group_id/messages => MessagesController#create
# And current person association remains safe to currently logged in person
# without being revealed thru parameters and such.

<% form_for([@group,@message]) do |f| %>
  <%= f.text_area :content %> 
  <%= f.submit "Submit" %>
<% end %>


def create
  @group = Group.find(params[:group_id])
  @person = current_person # or however you find current_person
  @message = @group.messages.build(params[:messaage])
  @message.person = @person
  if @message.save
    redirect_to(:back)
  else
    redirect_to(:back)
  end
end
笨死的猪 2024-11-16 10:14:37

尝试更改

 @message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person))


@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person), :content => params[:content])

Try changing

 @message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person))

to
@message = Message.create(:group => Group.find(params[:group_id]), :person => Person.find(current_person), :content => params[:content])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文