简体   繁体   中英

Can't Access Variables

I have a Request model that stores the gender, fitness level, and availability of a person. 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 . This doesn't work and Rails says unexpected keyword. I'm basically trying to pass the user.id of the Request object to the mail controller send_email action. Here it will use the user_id to find the user and then from there I will find the email of the user. 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

<% 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

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

In your case, you need to extract the user from the params hash:

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

Update:

You should define a nested resource .

config/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

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

Update your controller.

app/controllers/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

Simply you can try thi by passing user to the function

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

In routes config/routes.rb

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM