简体   繁体   English

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

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

I have a controller, farms_controller , that is being used for two different routes, farms and person_farms ; 我有一个控制器farms_controller ,它用于两条不同的路线farmsperson_farms the two routes use the same folder for view templates (ie, new_person_farm_path and new_farm_path use the same template, new.html.erb ). 这两个路由使用相同的文件夹作为视图模板(即, new_person_farm_pathnew_farm_path使用相同的模板new.html.erb )。 I want to access the farms register pages both separately and inside the person register, where some functionalities are different using the URL in some tests to show different functionalities (via request.env['PATH_INFO'].include? 'person_farms' , because I don't know a best way). 我想分别访问农场注册页面,也要访问个人注册内部页面,其中某些功能在某些测试中使用URL有所不同,以显示不同的功能(通过request.env['PATH_INFO'].include? 'person_farms' ,因为我不知道最好的方法)。

When I submit without the required fields (name) from the new_person_farm_path , the create function renders the new_farm_path page with a different styling for incorrect fields (using the 'twitter-bootstrap-rails' gem). 当我提交的new_person_farm_path没有必填字段(名称)时,create函数将使用不正确的字段的不同样式来呈现new_farm_path页面(使用'twitter-bootstrap- new_farm_path )。 I want it to instead render the new_person_farm_path page, with the same template file but a different routing (different URL). 我希望它改为呈现new_person_farm_path页面,该页面具有相同的模板文件,但具有不同的路由(不同的URL)。 I tried to use redirect_to and it shows the page in the correct URL, but not the styling in the wrong fields. 我尝试使用redirect_to ,它以正确的URL显示页面,但没有在错误的字段中显示样式。

However, all the instructions I saw in Rails documentation for rendering are above rendering a specific file, but it's not what I need. 但是,我在Rails文档中看到的所有渲染指令均高于渲染特定文件,但这不是我所需要的。 I have a feeling that it's not the "Rails way", but I'm a starter on RoR and this a legacy system that is being rewritten to Rails, so I can't correct the DB logic for now. 我感觉这不是“ Rails方式”,但我是RoR的入门者,而这是一个已被重写为Rails的旧系统,因此我现在无法纠正DB逻辑。

So, is there a way to render a view from same controller but with a different route? 那么,有没有一种方法可以从相同的控制器渲染一条具有不同路线的视图?


My code: 我的代码:

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

Send parameters in the path like: 在路径中发送参数,例如:

new_person_farm_path(param_1: 'my param')

And then in the controller you can access that value with: 然后在控制器中,您可以通过以下方式访问该值:

params[:param_1]

And pass it to your view as an instance variable or (if you are using) a view object: 并将其作为实例变量或(如果使用)视图对象传递给视图:

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

Now in your view you can ask for that value 现在,您认为您可以要求该值

<%= something if @show_some_part %>

For solving my specific problem (redirect to the correct new page but with the error fields), I did this solution (probably not the best if you are doing it from the start, but I was already with many functionalities working, and I wanted to keep the person_farm_path and farm_path as different URLs): 为了解决我的特定问题(重定向到正确的new页面,但有错误字段),我做了此解决方案(如果您从一开始就这样做可能不是最好的,但是我已经可以使用许多功能了,我想将person_farm_pathfarm_path为不同的URL):

In my controller, I used the first answer to this thread: Rails: I cant pass a validation error in a redirect , and so I inserted this code in the create action: 在我的控制器中,我使用了该线程的第一个答案: 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

Now I'm able to pass the errors through the redirect action, instead of the render action. 现在,我可以通过重定向操作(而不是渲染操作)传递错误了。 Then, in the form.html.erb , I only need do "explode" the string sent in a new array: 然后,在form.html.erb ,我只需要“ form.html.erb ”在新数组中发送的字符串:

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

For seeing all the errors_message I can get, I used <%= errors_sent_via_flask .inspect %> (this echoes all array members in the page) and simulated the error situation and submitted the form. 为了查看我可以得到的所有errors_message,我使用了<%= errors_sent_via_flask .inspect %> (这将回显页面中的所有数组成员),并模拟了错误情况并提交了表单。 For example, the error message for a missing name field is something like "Farm name can't be blank". 例如,缺少名称字段的错误消息类似于“农场名称不能为空”。

Now I can check if the "Farm name can't be blank" error is happening using 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" errors_sent_via_flask.include? "Farm name can't be blank" in the page. errors_sent_via_flask.include? "Farm name can't be blank"

暂无
暂无

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

相关问题 Ruby on Rails 3:尝试从不同的控制器渲染视图。 但我到同一个控制器 - Ruby on Rails 3 : Trying to render a view from a different controller. But I get to the same controller Ruby on Rails:如何使用不同的控制器从一个视图/页面链接/路由到另一个视图/页面 - Ruby on Rails: How to link/route from one view/page to another view/page with a different controller 如何在 Ruby on Rails 5.2 中将来自不同 controller 的视图渲染为模态 window? - How to render a view from a different controller as modal window in Ruby on Rails 5.2? 为不同的用户Rails红宝石呈现相同URL的不同控制器 - Render different controller for the same url for different user rails ruby 如何从控制器路线Rails 4进入不同视图 - How to get to different and multiple view from controller route Rails 4 如何将一个控制器的局部视图渲染到轨道上的ruby中的另一个控制器 - How to render partial view of one controller to another controller in ruby on rails Ruby on rails从视图路由到控制器中的自定义方法 - Ruby on rails route to a custom method in the controller from the view Rails 4-如何基于当前路径显示同一控制器动作的不同视图? - Rails 4 - How do I show a different view for the same controller action, based on the current route? Ruby on Rails,如何让控制器在不重定向的情况下呈现不同的页面? - Ruby on Rails, How to make a controller render different page without redirect? 如何从其他控制器渲染视图? - How to render view from of a different controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM