轨;从URL获取param值

时间:2016-03-21 12:22:58

标签: ruby-on-rails ruby

我创建了一个网站

  

本地主机:3000 /仪表板

现在,如果我点击此网站上的某个项目

<%= link_to project.project_name, :project_id => project.id, :method => :post %>

我使用网址

中的参数保留在同一网站上(我想要的)
  

本地主机:3000 /仪表盘方法=交&安培; PROJECT_ID = 1

如果我点击该项目,我将获得该参数。
我在我的控制器中试过跟随: @project_id = params[:project_id]

但我得到的一切都是零级,也许你们中的某个人可以帮助我

更新

控制器:

    def index 
        @project = DashboardInput.where(project_leader_id: current_employee.personal_id)
        #projects = Project.all

        project_id = 1 #value has to be changed; hard coded only for testing
        #project_id = params[:project_id]
        @selected_project = DashboardInput.find(project_id)
        @project_name = DashboardInput.find(project_id).project_name

        project_leader_id = DashboardInput.find(project_id).project_leader_id
        leader = Employee.where(personal_id: project_leader_id)
        leader_first_name = leader.pluck(:first_name)[0]
        leader_last_name = leader.pluck(:last_name)[0]
        @project_leader_for_routing = Employee.find_by personal_id: project_leader_id
        @leader_name = leader_first_name + " " +leader_last_name

        @startdate = DashboardInput.find(project_id).start_date
        @enddate = DashboardInput.find(project_id).end_date
        @cost_center = DashboardInput.find(project_id).cost_center_id
        @wbs = DashboardInput.find(project_id).wbs

        @employees_test = Employee.all

            @test_param = params[:project_id]
end


的index.html

<div class="col-md-3">
<div class="row paragraphline">
    <h1>&nbsp;&nbsp;Project Status</h1>
    <h1><%= @test_param %></h1>
</div>
<div class="row showtables">
    <table id="assignments" class="display table-hover"> 
        <thead>
            <tr>
                <th>Status</th>
                <th>Project Name</th>
            </tr>
        </thead>
        <tbody>

        <% @project.each do |project| %>

            <tr>
                <% if project.project_name == "Testproject_Dashboard" %>
                    <td><div style="display:none;">3</div><%=image_tag("Ampel_Red.PNG", size: "25x25", class: "logo")%></td>
                <% elsif project.project_name == "Testproject_dashboard_2" %>
                    <td><div style="display:none;">2</div><%=image_tag("Ampel_Yellow.PNG", size: "25x25", class: "logo")%></td>
                <% else %>
                    <td><div style="display:none;">1</div><%=image_tag("Ampel_Green.PNG", size: "25x25", class: "logo")%></td>
                <% end %>
                <td><%= link_to project.project_name, :project_id => project.id, :method => :post%></td>
            </tr>

        <% end %>

        </tbody>
    </table>
</div>


的routes.rb

Rails.application.routes.draw do

  resources :dashboard_inputs
  get 'delete_project_leader_status/destroy'

  get 'delete_project_leader_status/index'

  get 'delete_project_leader_status/destroy'

  get 'delete_prject_leader/destroy'

  get 'management/show'
  get 'management/wbs'
  get 'management/assignment'
  get 'management/extract'


  get 'static_pages/home'
  get 'static_pages/help'
  get 'static_pages/about'
  get 'static_pages/login'

  root 'static_pages#home'


  get 'login' => 'sessions#new'
  post 'login' =>'sessions#create'
  get 'change' =>'sessions#change_password'
  post 'change' =>'sessions#change'
  get 'logout' => 'sessions#destroy'


  get 'home' => 'static_pages#home'
  get 'functions' => 'functions#index'
  get 'cost_centers' => 'cost_centers#index'
  get 'topics' => 'topics#index'
  get 'project_statuses' => 'project_statuses#index'
  get 'providers' => 'providers#index'
  #get 'roles' => 'roles#index'
  get 'projects' => 'projects#index'
  get 'employees' => 'employees#index'
  get 'assignments' => 'assignments#index'
  post 'assignments/create' => 'assignments#before_create'
  get 'assignments/update_subprojects', as: 'update_subprojects' 
  get 'management' => 'managementshow'
  get 'dashboard' => 'management_dashboard#index'
  get 'dashboard/detail' => 'management_dashboard#detail'
  get 'pj_leader' => 'delete_project_leader_status#index'
  get 'reports' => 'reports#index'
  get 'etc' => 'reports#etc'
  get "help" => 'static_pages#help'
  get "dashboard_input" => 'dashboard_inputs'
  get "inputtest" => 'management_dashboard#input_test'

  resource :reports
  resources :bookings
  resource :delete_project_leader_status
  resources :topics
  resources :cost_centers
  #resources :roles
  resources :providers
  resource :project_statuses
  resources :assignments
  resources :employees
  resources :sub_projects
  resources :projects do
    collection { post :import }
  end
  resources :management_dashboard  
  resources :cost_centers
  resources :project_statuses
  resources :assignment_statuses
  resources :bookings do
    collection { post :import }
  end
end

1 个答案:

答案 0 :(得分:0)

由于代码中的nil:NilClass,您收到@project_id_test。你没有定义它。

您的index.html.erb

中未定义此行

<h1><%= @project_id_test %></h1>

将其更改为已定义的内容。

更新:

在您的索引控制器中,使用@test_param初始化params[:project_id],并且在您的dashboard#index首次加载时,params[:project_id]未定义在您的网址中的任何位置(或文本字段,如果你有)。所以这会导致错误。

尝试检查params[:project_id]是否有值:

params[:project_id].nil? ? @test_param = 'something code here' : @test_params = params[:project_id]