Ruby on rails,发送帖子表单后的路径

时间:2021-12-09 07:34:41

I have a form on /contact/new, and when I click a Send, but user did not fill some text_field after verification on server (if @contact.valid?), user is being redirected to /contact/.

我在/ contact / new上有一个表单,当我点击发送,但用户在服务器上验证后没有填写一些text_field(如果@ contact.valid?),则用户被重定向到/ contact /。

I have a navigation bar, and then it fails in highlighting.

我有一个导航栏,然后突出显示失败。

<% if current_page?(new_contact_path) %>
    <li class="hover-mouse active-menu"><%= link_to "Contact", new_contact_path%></li>
<% else %>
    <li class="hover-mouse"><%= link_to "Contact", new_contact_path%></li>
<% end %>

Does somebody knows how to fix that?

有人知道如何解决这个问题吗?

1 个解决方案

#1


0  

That's because when you hit submit button of your form, it takes you to the create action and since your haven't filled your form it simply renders back your form but you are still on the create method and hence your condition fails For details you should checkout this answer

那是因为当你点击表单的提交按钮时,它会将你带到创建操作,因为你还没有填写表单,它只是渲染你的表单,但你仍然在创建方法,因此你的条件失败。结帐这个答案

Fix

You can't use current_page? helper because your create method and index have same path helpers(only http verb changes) so if you'll use contacts_path in your current_page it'll show up active class in index action also so you should use rails action_name helper will check for your action name and if it's new action or create action then it will apply active class on your li

你不能使用current_page? helper因为你的create方法和索引有相同的路径助手(只有http动词改变)所以如果你在current_page中使用contacts_path它也会在索引动作中显示活动类,所以你应该使用rails action_name helper将检查你的动作如果它是新动作或创建动作,那么它将在你的li上应用活动类

<% if action_name == "new" || action_name == "create" %>
  <li class="hover-mouse active-menu"><%= link_to "Contact", new_contact_path%></li>
<% else %>
  <li class="hover-mouse"><%= link_to "Contact", new_contact_path%></li>
<% end %>

#1


0  

That's because when you hit submit button of your form, it takes you to the create action and since your haven't filled your form it simply renders back your form but you are still on the create method and hence your condition fails For details you should checkout this answer

那是因为当你点击表单的提交按钮时,它会将你带到创建操作,因为你还没有填写表单,它只是渲染你的表单,但你仍然在创建方法,因此你的条件失败。结帐这个答案

Fix

You can't use current_page? helper because your create method and index have same path helpers(only http verb changes) so if you'll use contacts_path in your current_page it'll show up active class in index action also so you should use rails action_name helper will check for your action name and if it's new action or create action then it will apply active class on your li

你不能使用current_page? helper因为你的create方法和索引有相同的路径助手(只有http动词改变)所以如果你在current_page中使用contacts_path它也会在索引动作中显示活动类,所以你应该使用rails action_name helper将检查你的动作如果它是新动作或创建动作,那么它将在你的li上应用活动类

<% if action_name == "new" || action_name == "create" %>
  <li class="hover-mouse active-menu"><%= link_to "Contact", new_contact_path%></li>
<% else %>
  <li class="hover-mouse"><%= link_to "Contact", new_contact_path%></li>
<% end %>