Ruby-on-rails Rails:“新建或编辑”路径助手?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5625323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:54:13  来源:igfitidea点击:

Rails: "new or edit" path helper?

ruby-on-railsruby-on-rails-3new-operatoreditlink-to

提问by Andrew

Is there a simple and straightforward way to provide a link in a view to either create a resource if it doesn't exist or edit the existing on if it does?

是否有一种简单而直接的方法可以在视图中提供链接,以便在资源不存在时创建资源或在存在时编辑现有资源?

IE:

IE:

User has_one :profile

Currently I would be doing something like...

目前我会做类似的事情......

-if current_user.profile?
  = link_to 'Edit Profile', edit_profile_path(current_user.profile)
-else
  = link_to 'Create Profile', new_profile_path

This is ok if it's the only way, but I've been trying to see if there's a "Rails Way" to do something like:

如果这是唯一的方法,这还可以,但是我一直在尝试查看是否有“Rails 方式”可以执行以下操作:

= link_to 'Manage Profile', new_or_edit_path(current_user.profile)

Is there any nice clean way to do something like that? Something like the view equivalent of Model.find_or_create_by_attribute(....)

有什么好的干净的方法来做这样的事情吗?类似于视图的东西Model.find_or_create_by_attribute(....)

回答by Douglas F Shearer

Write a helper to encapsulate the more complex part of the logic, then your views can be clean.

写一个助手来封装逻辑中更复杂的部分,然后你的视图就可以干净了。

# profile_helper.rb
module ProfileHelper

  def new_or_edit_profile_path(profile)
    profile ? edit_profile_path(profile) : new_profile_path(profile)
  end

end

Now in your views:

现在在你看来:

link_to 'Manage Profile', new_or_edit_profile_path(current_user.profile)

回答by Russell

I came across this same problem, but had a lot of models I wanted to do it for. It seemed tedious to have to write a new helper for each one so I came up with this:

我遇到了同样的问题,但我有很多模型想要这样做。必须为每个助手编写一个新助手似乎很乏味,所以我想出了这个:

def new_or_edit_path(model_type)
  if @parent.send(model_type)
    send("edit_#{model_type.to_s}_path", @parent.send(model_type))
  else
    send("new_#{model_type.to_s}_path", :parent_id => @parent.id)
  end
end

Then you can just call new_or_edit_path :childfor any child of the parent model.

然后您可以调用new_or_edit_path :child父模型的任何子模型。

回答by krunal shah

Another way!

其它的办法!

  <%=
     link_to_if(current_user.profile?, "Edit Profile",edit_profile_path(current_user.profile)) do
       link_to('Create Profile', new_profile_path)
     end
  %>

回答by MurifoX

If you want a generic way:

如果你想要一个通用的方式:

def new_or_edit_path(model)
  model.new_record? ? send("new_#{model.model_name.singular}_path", model) : send("edit_#{model.model_name.singular}_path", model)
end

Where modelis your instance variable on your view. Example:

model您的视图中的实例变量在哪里。例子:

# new.html.erb from users
<%= link_to new_or_edit_path(@user) do %>Clear Form<% end %>

回答by gandham varesh kumar

Try this:

尝试这个:

module ProfilesHelper

  def new_or_edit_profile_path(profile)
    profile ? edit_profile_path(profile) : new_profile_path(profile)
  end

end

and with your link like:

并使用您的链接:

<%= link_to 'Manage Profile', new_or_edit_profile_path(@user.profile) %>