本文介绍了如何Rails的导入现有行,当你与QUOT;编辑"在脚手架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常糟糕的措辞问题,但我很好奇Rails如何知道把表单数据,当您单击编辑。举例来说,如果我脚手架一个名为Post模型,并输入了一个标题和内容的帖子,后来当我编辑,表单自动填充。

That was probably a very badly worded question, but I'm curious how Rails knows to bring in the form data when you click edit. For example, if I scaffolded a model called Post, and typed in a title and content for that Post, later when I edit it, the form is automatically filled in.

我看着控制器和部分的形式,但它似乎并不像它包含什么,告诉它填补它与现有的数据。

I looked at the controller and the form partial, but it doesn't seem like it contains anything to tell it to fill it up with the existing data.

我问这个的原因是因为我想允许用户以进口其他人的帖子进入其形式和编辑。我将如何做到这一点?

The reason I am asking this is because I want to allow users to "import" other people's posts into their form and edit it. How would I do this?

推荐答案

要回答你的问题的第一部分,通过查看链接编辑您的模型开始 - 在索引视图文件(应用/views/posts/index.html.erb )它看起来像:

To answer the first part of your question, start by looking at the link to edit your model - in the index view file (app/views/posts/index.html.erb) it will look like:

<%= link_to 'Edit', edit_post_path(post) %>

当您在浏览器中打开索引页面,在此建立像一个链接:

When you open the index page in your browser, this builds a link like:

http://*servername*/post/*id*/edit

资源路由知道该调用修改 post_controller ,而在<$ C $的作用C> ID 值是在URL中。

The resource routing knows that this calls the edit action of the post_controller, and that the id value is in the URL.

现在来看看在修改 post_controller 行动应用程序/控制器/posts_controller.rb ) - 它看起来像:

Now take a look at the edit action in your post_controller (app/controllers/posts_controller.rb) - it looks like:

def edit
  @post = Post.find(params[:id])
end

这一切确实是使用 ID 值是通过URL传递到找到发表数据库与 ID ,并将其保存在变量 @post 。如果你在编辑视图文件的code(应用程序/视图/职位/ edit.html.erb )也大多只是使用部分形式( /app/views/posts/_form.html.erb ),而该文件包含code呈现在你的浏览器中看到的实际形式。

all this does is use the id value that was passed through the URL to find the Post in the database with that id, and save it in the variable @post.If you look in the code of the edit view file (app/views/posts/edit.html.erb) it mostly just uses the form partial (/app/views/posts/_form.html.erb) and that file contains the code for rendering the actual form you see in the browser.

<%= form_for(@post) do |f| %>

这行开始使用得到的 PostsController <$ C $设置 @post 变量构建形式C>修改的行动。此code块里面,你看到的东西一样 -

This line starts building the form using the @post variable that got set in the PostsController edit action. Inside the block for this code, you see things like -

<div class="field">
  <%= f.label :title %>
  <%= f.text_field :title %>
</div>

这code建立在绑在形式的文本字段: @post 标题属性 - 如果一个值的属性存在,则 text_field 方法知道填充文本字段的值

This code builds a text field in the form tied to the :title property of @post - if a value exists in that property, the text_field method knows to populate the text field with that value.

第二部分(我希望让用户进口其他人的帖子进入其形式和编辑)是一个很大的开放性,并更多地取决于你真正想做的事在您的应用程序 - 你可以只允许所有用户看到所有其他用户的帖子,并直接编辑它们(有点像一个wiki,但破坏性编辑)。或者你可以让帖子编辑只能由创建它们的用户,但可以查看其他人,然后你可以创建一个控制器动作,允许用户对其他用户的帖子的内容复制到当前用户拥有一个新的职位。或者其他的东西 - 它真的只是取决于你想要什么

The second part ('I want to allow users to "import" other people's posts into their form and edit it') is a lot more open-ended and depends more on what you actually want to do in your app - you could just allow all users to see all other users posts and edit them directly (kind of like a wiki, but with destructive edits). Or you could make posts editable only by the user that created them, but viewable by others, and then you could create a controller action that allows a user to copy the contents of another user's post into a new post owned by the current user. Or other things - it really just depends what you want.

这篇关于如何Rails的导入现有行,当你与QUOT;编辑&QUOT;在脚手架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:26