繁体   English   中英

Ruby on Rails:如何从相同的控制器渲染具有不同路线的视图?

[英]Ruby on Rails: how to render a view from same controller but with a different route?

我有一个控制器farms_controller ,它用于两条不同的路线farmsperson_farms 这两个路由使用相同的文件夹作为视图模板(即, new_person_farm_pathnew_farm_path使用相同的模板new.html.erb )。 我想分别访问农场注册页面,也要访问个人注册内部页面,其中某些功能在某些测试中使用URL有所不同,以显示不同的功能(通过request.env['PATH_INFO'].include? 'person_farms' ,因为我不知道最好的方法)。

当我提交的new_person_farm_path没有必填字段(名称)时,create函数将使用不正确的字段的不同样式来呈现new_farm_path页面(使用'twitter-bootstrap- new_farm_path )。 我希望它改为呈现new_person_farm_path页面,该页面具有相同的模板文件,但具有不同的路由(不同的URL)。 我尝试使用redirect_to ,它以正确的URL显示页面,但没有在错误的字段中显示样式。

但是,我在Rails文档中看到的所有渲染指令均高于渲染特定文件,但这不是我所需要的。 我感觉这不是“ Rails方式”,但我是RoR的入门者,而这是一个已被重写为Rails的旧系统,因此我现在无法纠正DB逻辑。

那么,有没有一种方法可以从相同的控制器渲染一条具有不同路线的视图?


我的代码:

def create
  @farm = Farm.new(farm_params)
  respond_to do |format|
    if @farm.save
      # Param sent by the page to test if I'm in a person_farms page
      # instead of a farms page because the request.env test doesn't work here.
      # I feel that this is not the correct way to do that, but I can leave the correct way to another question.
      # However, if someone has a suggestion a comment would be appreciated.
      if params[:is_person_farm_page]
        format.html { redirect_to @person_farm_path(@farm), notice: 'Farm saved' }
        format.json { render :show, status: :created, location: @farm }
      else
        format.html { redirect_to @farm, notice: 'Farm saved' }
        format.json { render :show, status: :created, location: @farm }
      end
    else
      #This is the point where I want to redirect to new_person_farm_path
      #I would need to test using the params[:is_person_farm_page] above
      format.html { render :new }
      format.json { render json: @farm.errors, status: :unprocessable_entity }
    end
  end
end

在路径中发送参数,例如:

new_person_farm_path(param_1: 'my param')

然后在控制器中,您可以通过以下方式访问该值:

params[:param_1]

并将其作为实例变量或(如果使用)视图对象传递给视图:

@show_some_part = params[:param_1].present? # This can be a boolean or something else

现在,您认为您可以要求该值

<%= something if @show_some_part %>

为了解决我的特定问题(重定向到正确的new页面,但有错误字段),我做了此解决方案(如果您从一开始就这样做可能不是最好的,但是我已经可以使用许多功能了,我想将person_farm_pathfarm_path为不同的URL):

在我的控制器中,我使用了该线程的第一个答案: Rails:我无法在redirect中传递验证错误 ,因此我将此代码插入了create动作:

if @farm.save
  ... #the same code in my question
else
  if params[:is_person_farm_page]
    #pass the errors messages via flash, "imploding" the array of errors into a string
    format.html { redirect_to new_person_farm_path, :flash => { :error => flash_errors = @farm.errors.full_messages.join(',')}}
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  else
    format.html { render :new }
    format.json { render json: @farm.errors, status: :unprocessable_entity }
  end
end

现在,我可以通过重定向操作(而不是渲染操作)传递错误了。 然后,在form.html.erb ,我只需要“ form.html.erb ”在新数组中发送的字符串:

<% errors_sent_via_flask = flash[:error] ? flash[:error].split(",") : [] %>

为了查看我可以得到的所有errors_message,我使用了<%= errors_sent_via_flask .inspect %> (这将回显页面中的所有数组成员),并模拟了错误情况并提交了表单。 例如,缺少名称字段的错误消息类似于“农场名称不能为空”。

现在,我可以使用errors_sent_via_flask.include? "Farm name can't be blank"检查“农场名称不能为空”错误errors_sent_via_flask.include? "Farm name can't be blank" errors_sent_via_flask.include? "Farm name can't be blank"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM