为什么链接不起作用?未定义的局部变量路径或方法ROR

时间:2021-07-08 23:54:49

I cannot get my link to work: Users can post job listings. Other users can view the job listing and click on apply. Now a form should appear for the user to fill out some information. However I cannot get the path and route file right. This is the link in show.html.erb that's now working

我无法获得工作链接:用户可以发布工作列表。其他用户可以查看职位列表并单击“申请”。现在应该出现一个表单供用户填写一些信息。但是我无法正确获取路径和路径文件。这是show.html.erb中现在正在运行的链接

<li><%= link_to "Apply Now", listing_apply_path(id: listings.id) %></li>

The form is under views/listings/apply.html.erb

该表单位于views / listings / apply.html.erb下

listings/show.html.erb

列表/ show.html.erb

<div class="show_listing">

    <div class="col-md-6">
    <div class="col-md-6">
        <h3><%= @listing.title %></h3>
        <h3><%= @listing.location %></h3>
        <p><%= @listing.description %></p><br>
        <div class="center">
        <li><%= link_to "Apply Now", listing_apply_path(id: listings.id) %></li>
    </div>
    </div>
</div>
</div>

<div class="show_link_position">
<% if current_user == @listing.user %>
<%= link_to 'Edit', edit_listing_path, class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', current_user, class: "btn btn-link" %>
</div>

listings_controller.rb

listings_controller.rb

    def apply
        @listing = Listing.find(params[:id])
      end

and routes

和路线

Rails.application.routes.draw do

  resources :categories

  get 'notifications/index'

  get 'notifications/create'

    resources :users
    resources :sessions, only: [:new, :create, :destroy]
    resources :listings



  root 'static_pages#home'


      match '/signup',  to: 'users#new',            via: 'get'
      match '/signin',   to: 'sessions#new',          via: 'get'
      match '/signout', to: 'sessions#destroy',     via:'delete'
      match '/help',      to: 'static_pages#help',    via: 'get'
      match '/contact',   to: 'static_pages#contact',  via: 'get'
      match '/about',     to: 'static_pages#about',   via: 'get' 
      match '/new',     to: 'listings#new',   via: 'get' 
      match '/users/:name/:id', to: 'listings#show', via: :get, as: :user_listing
      match '/findjobs',     to: 'listings#index',   via: 'get' 
      match '/users/:name/:id/edit', to: 'listings#edit', via: 'get'
      match '/:id/apply', to:  'listings#apply', via: 'get'

I appreciate your help!

我感谢您的帮助!

2 个解决方案

#1


2  

While Coderhs has added something for you you also need to name that route so that you have a matcher for it like this:

虽然Coderhs已经为你添加了一些东西,你还需要命名那条路线,这样你就可以像这样找到一个匹配器:

 match '/:id/apply', to:  'listings#apply', via: 'get', as: :listing_apply

that way you will have access to the url helper listing_apply_path

这样你就可以访问url helper listing_apply_path

also match and via is not needed you can just specify

也不需要匹配和通过你可以指定

get '/:id/apply', to: 'listings#apply', as: :listing_apply

match means any method (e.g. POST,GET,PUT,DELETE)

匹配意味着任何方法(例如POST,GET,PUT,DELETE)

get means process GET requests.

get意味着处理GET请求。

match with :via means the same thing as get without it. I only use via in cases where I need 2 or more VERBS to got to the same place. Otherwise I just specify the method needed.

匹配:通过与没有它相同的东西。我只在需要2个或更多VERBS到达同一个地方的情况下使用via。否则我只需指定所需的方法。

From http://guides.rubyonrails.org/routing

来自http://guides.rubyonrails.org/routing

Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.

将GET和POST请求路由到单个操作具有安全隐患。通常,除非有充分的理由,否则应避免将所有动词都路由到某个动作。

Update

更新

You asked

你问

Why do I need to tell routes here that I need the path as:listing_apply. Why didn't I have to do it for lets say the edit path?

为什么我需要在这里告诉我需要路径的路由:listing_apply。为什么我不能这样做让我们说编辑路径?

the resources method creates these for you

资源方法为您创建这些

resources :listings

generates following routes for you

为您生成以下路线

listings     GET    /listings              listings#index
             POST   /listings/create       listings#create
new_listing  GET    /listings/new          listings#new
edit_listing GET    /listings/:id/edit     listings#edit
listing      GET    /listings/:id          listings#show
             PUT    /listings/:id          listings#update
             DELETE /listings/:id          listings#destroy

This will allow access to listings_path, new_listing_path, edit_listing_path, and listing_path. For the non named routes it uses the VERB (POST, PUT, DELETE) to determine routing

这将允许访问listings_path,new_listing_path,edit_listing_path和listing_path。对于非命名路由,它使用VERB(POST,PUT,DELETE)来确定路由

#2


1  

It should be

它应该是

<li><%= link_to "Apply Now", listing_apply_path(id: @listing.id) %></li>

you need to pass in the instance variable into the path, using listing is undefined inside of the view.

您需要将实例变量传递到路径中,使用列表在视图内部未定义。

#1


2  

While Coderhs has added something for you you also need to name that route so that you have a matcher for it like this:

虽然Coderhs已经为你添加了一些东西,你还需要命名那条路线,这样你就可以像这样找到一个匹配器:

 match '/:id/apply', to:  'listings#apply', via: 'get', as: :listing_apply

that way you will have access to the url helper listing_apply_path

这样你就可以访问url helper listing_apply_path

also match and via is not needed you can just specify

也不需要匹配和通过你可以指定

get '/:id/apply', to: 'listings#apply', as: :listing_apply

match means any method (e.g. POST,GET,PUT,DELETE)

匹配意味着任何方法(例如POST,GET,PUT,DELETE)

get means process GET requests.

get意味着处理GET请求。

match with :via means the same thing as get without it. I only use via in cases where I need 2 or more VERBS to got to the same place. Otherwise I just specify the method needed.

匹配:通过与没有它相同的东西。我只在需要2个或更多VERBS到达同一个地方的情况下使用via。否则我只需指定所需的方法。

From http://guides.rubyonrails.org/routing

来自http://guides.rubyonrails.org/routing

Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.

将GET和POST请求路由到单个操作具有安全隐患。通常,除非有充分的理由,否则应避免将所有动词都路由到某个动作。

Update

更新

You asked

你问

Why do I need to tell routes here that I need the path as:listing_apply. Why didn't I have to do it for lets say the edit path?

为什么我需要在这里告诉我需要路径的路由:listing_apply。为什么我不能这样做让我们说编辑路径?

the resources method creates these for you

资源方法为您创建这些

resources :listings

generates following routes for you

为您生成以下路线

listings     GET    /listings              listings#index
             POST   /listings/create       listings#create
new_listing  GET    /listings/new          listings#new
edit_listing GET    /listings/:id/edit     listings#edit
listing      GET    /listings/:id          listings#show
             PUT    /listings/:id          listings#update
             DELETE /listings/:id          listings#destroy

This will allow access to listings_path, new_listing_path, edit_listing_path, and listing_path. For the non named routes it uses the VERB (POST, PUT, DELETE) to determine routing

这将允许访问listings_path,new_listing_path,edit_listing_path和listing_path。对于非命名路由,它使用VERB(POST,PUT,DELETE)来确定路由

#2


1  

It should be

它应该是

<li><%= link_to "Apply Now", listing_apply_path(id: @listing.id) %></li>

you need to pass in the instance variable into the path, using listing is undefined inside of the view.

您需要将实例变量传递到路径中,使用列表在视图内部未定义。