简体   繁体   English

无法访问变量

[英]Can't Access Variables

I have a Request model that stores the gender, fitness level, and availability of a person. 我有一个Request模型,用于存储性别,适合水平和一个人的可用性。 This object belongs to a User model. 该对象属于用户模型。 What I'm trying to do is pass the user id of the person who originally made the Request to the and sends the :user parameter to the action send_mail on the mail controller when the user clicks the button . 我想要做的是将最初发出请求的用户的用户ID传递给,然后在用户单击按钮时将:user参数发送到邮件控制器上的send_mail操作。 This doesn't work and Rails says unexpected keyword. 这不起作用,Rails说出了意外的关键字。 I'm basically trying to pass the user.id of the Request object to the mail controller send_email action. 我基本上是试图将Request对象的user.id传递给邮件控制器send_email操作。 Here it will use the user_id to find the user and then from there I will find the email of the user. 在这里它将使用user_id查找用户,然后从那里找到该用户的电子邮件。 I will then send the user an email with this variable. 然后,我将向用户发送包含此变量的电子邮件。

Request Controller 请求控制器

class RequestsController < ApplicationController
before_action :authenticate_user!

def index
    @requests = Request.all
end

def create
    @request = Request.new(request_params)
    @request.user = current_user
    @request.save
    redirect_to requests_path 
end

def new
    @request = Request.new
end

def destroy
end

private 
    def request_params
        params.require(:request).permit(:gender, :level, :time_available)
    end
end

Index.html.erb Index.html.erb

<% provide(:title, "Open Requests") %>
<table class="table table-hover">
  <th>Gender</th>
  <th>Level</th>
  <th>Time Available: </th>
  <% @requests.each do |request| %>
  <tr>
    <td><%= request.gender %></td>
    <td><%= request.level %></td>
    <td><%= request.time_available %></td>
    <td><%= button_to "I want to exercise with you!", {:controller => "mail", :action => "send_email", :user => Request.user }  class: "btn btn-primary" %></td>
  </tr>
<% end %>
</table>

Mail Controller 邮件控制器

class MailController < ApplicationController
    def send_email
        @creation_user = :user
        @email = @creation_user.email
    end
end

Send_Email.html.erb Send_Email.html.erb

<h2><%= @email %></h2>

In your case, you need to extract the user from the params hash: 在您的情况下,您需要从params哈希中提取用户:

def send_email
  @creation_user = params[:user]
  @email = @creation_user.email
end

Update: 更新:

You should define a nested resource . 您应该定义一个嵌套资源

config/routes.rb 配置/ routes.rb中

resources :requests, only: [:index, :new, :create] do
  resources :mails, only: [:create]
end

Now update your view to use your new route. 现在更新您的视图以使用新路线。

app/views/requests/index.html.erb 应用程序/视图/请求/ index.html.erb

<%= button_to "I want to work out with you!", request_mails_path(request) %>

Update your controller. 更新您的控制器。

app/controllers/mails_controller.rb 应用程序/控制器/ mails_controller.rb

def create
  @email = user.email
end

private

def user
  find_request.user
end

def find_request
  Request.find(params[:request_id])
end

Rename your view send_email.html.erb to create.html.erb 将您的视图send_email.html.erb重命名为create.html.erb

Simply you can try thi by passing user to the function 只需将user传递给函数即可尝试

 class MailController < ApplicationController
    def send_email
        @creation_user = params[:user]
        @email = @creation_user.email
    end
end

In routes config/routes.rb 在路由中config/routes.rb

get "mail/send_mail", to: 'mail#send_mail'

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

相关问题 无法访问Zurb Foundation的全局变量 - Can't access Zurb Foundation global variables 为什么我不能在Ruby中访问块中的实例变量? - Why can't I access instance variables inside block in Ruby? 无法从另一个模型访问一个模型的实例变量)(self.ruby) - Can't access one model's instance variables from another model) (self.ruby) 在Centos上部署的Rails应用(带有独立的Passenger和Capistrano)无法访问ENV变量 - Rails app deployed on Centos w/ standalone Passenger and Capistrano can't access ENV variables 具有手动数据库连接的ActiveRecord子类无法直接访问列变量 - ActiveRecord Subclass with manual DB connection can't directly access column variables bash中的ENV变量-为什么不能在Rails本地设置中访问它们? - ENV variables in bash - why can't I access them in my Rails local setup? 为什么我不能在 Active Record 回调中直接访问实例变量? - Why can't I directly access instance variables in Active Record callback? rails 5.2.0,sass-rails 5.0.7,引导程序4.3.1无法访问.scss文件中的引导程序变量 - rails 5.2.0, sass-rails 5.0.7, bootstrap 4.3.1 Can't access bootstrap variables within .scss files 您可以在rake任务中访问环境变量吗? - Can you access environment variables in a rake task? Rails…无法访问对象? - Rails … Can't access object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM