在Rails中渲染与我的视图冲突

时间:2017-12-07 03:08:08

标签: ruby-on-rails ruby

我从Ruby on Rails开始,我在Stack Overflow和Google中搜索,我找不到解决问题的方法。实际上发生了什么,我有我的默认视图application.html.erb,我限制了对某些部分验证的访问权限

我要做的是将validates方法放在模型company.rb中,当用户出错时,我的companies_controller.rb将为用户修复提供实际操作。< / p>

但是当我这样做时,application.html.erb中的验证无法将呈现的操作识别为companies_controller.rb

的一部分

application.html.erb

<body>
    <% if user_signed_in? %>
    <%= render 'shared/navbar' unless (controller.controller_name == "messages" && controller.action_name == "new") || (controller.controller_name == "links" && controller.action_name == "edit") || (controller.controller_name == "messages" && controller.action_name == "edit")  %>
    <%= render 'shared/sidebar' unless current_page?(root_path) || current_page?(edit_user_registration_path) || (controller.controller_name == "companies" && controller.action_name == "index") || (controller.controller_name == "companies" && controller.action_name == "new") || current_page?(new_company_path) || (controller.controller_name == "links" && controller.action_name == "edit") || (controller.controller_name == "messages" && controller.action_name == "new") || (controller.controller_name == "messages" && controller.action_name == "edit") %>
    <% end %>
    <%= yield %>
</body>
来自create

companies_controller.rb方法

def create
    @company = current_user.companies.new(company_params)

    respond_to do |format|
      if @company.save
        current_user.companies << @company
        format.html { redirect_to @company, notice: 'Company was successfully created.' }
        format.json { render :show, status: :created, location: @company }
      else
        format.html { render :new }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

_form.html.erb

<%= form_with(model: company, local: true) do |form| %>
  <% if company.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(company.errors.count, "error") %> prohibited this company from being saved:</h2>

      <ul>
      <% company.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name, id: :company_name, class: "form-control" %>
  </div>

  <div class="field">
    <%= form.label :phone %>
    <%= form.text_field :phone, id: :company_phone, class: "form-control" %>
  </div>

  <div class="field">
    <%= form.label :category %>
    <%= form.text_field :category, id: :company_category, class: "form-control" %>
  </div>

  <div class="field">
    <%= form.label :active %>
    <%= form.check_box :active %>
  </div>

  <div class="actions">
    <%= form.submit "Save", class: "btn btn-primary" %>
    <%= link_to 'Back', companies_path, class: "btn btn-default" %>
  </div>
<% end %>

company.rb

class Company < ApplicationRecord
    validates :name, uniqueness: true, presence: true

    has_many :folders, :dependent => :destroy
    has_many :analytics, :dependent => :destroy
    has_many :leads, :dependent => :destroy
    has_many :messages, :dependent => :destroy
    has_many :links, :dependent => :destroy

    has_many :user_companies, :dependent => :destroy
    has_many :users, :through => :user_companies
end

错误为ActionController::UrlGenerationError in Companies#create

有人可以帮忙吗?抱歉我的英文。

谢谢!

2008年12月更新。

我的routes.rb

scope '/dashboard' do
    resources :companies do
      resources :users
      resources :user_companies
      resources :folder_links
      resources :folders
      resources :analytics
      resources :leads
      resources :links
      resources :messages

      get 'user_companies/new/search', to: "user_companies#search"
    end
  end

2 个答案:

答案 0 :(得分:0)

我修复了错误,添加了另一个验证。发生的事情是我在void swap(string &a, string &b){ string t = a; a = b; b= t; } 上再错过了一次验证:

application.html.erb

我刚刚将条件添加到当前操作为<%= render 'shared/navbar' unless (controller.controller_name == "messages" && controller.action_name == "new") || (controller.controller_name == "links" && controller.action_name == "edit") || (controller.controller_name == "messages" && controller.action_name == "edit") %> <%= render 'shared/sidebar' unless current_page?(root_path) || current_page?(edit_user_registration_path) || (controller.controller_name == "companies" && controller.action_name == "index") || (controller.controller_name == "companies" && controller.action_name == "new") || current_page?(new_company_path) || (controller.controller_name == "links" && controller.action_name == "edit") || (controller.controller_name == "messages" && controller.action_name == "new") || (controller.controller_name == "messages" && controller.action_name == "edit") %> 的时候:

create

抱歉浪费你的时间。

感谢您帮助,伙计们!

答案 1 :(得分:0)

@Lucas虽然它解决了你的问题,但除非你提出条件,否则我不推荐使用long。我不知道你为什么要检查控制器动作。执行此操作的最佳方法是从控制器操作调用渲染。您可以优化此代码。

相关问题