Cette fois, je vais vous présenter comment stocker les informations saisies dans la zone de texte dans une variable de la méthode.
ruby:new.html.erb
<div class="infomation_new">
<%= form_tag("/infomations",method: :post) do |f|%>
<p class = "infomation_new_textarea"><%= text_field_tag :talent_name%></p></br>
<p class = "infomation_new_submit"><%= submit_tag 'Chercher'%></p>
<% end %>
</div>
L'accent est mis cette fois sur: talent_name à côté de text_field_tag. Faites cette description
infomations_controller.rb
def create
talent_name = params[:talent_name]
agent = Mechanize.new
personal_page = agent.get('https://talent-dictionary.com/' + talent_name)
aaas = personal_page.at('.talent_name_wrapper')
@ages = aaas.at('.age').inner_text.delete('âge').to_i if aaas.at('.age')
@names = aaas.at('h1').inner_text if aaas.at('h1')
@image_urls = personal_page.at('.main_image img').get_attribute('src') if personal_page.at('.main_image img')
@infomation = Infomation.where(name: @names).first_or_initialize
@infomation.age = @ages
@infomation.image_url = @image_urls
@infomation.save
end
Vous pouvez le stocker dans une variable de la méthode en définissant talent_name = params [: talent_name] sur la deuxième ligne. Par exemple, si vous entrez Taro Yamada
[16, 25] in /home/ec2-user/environment/filebook/app/controllers/infomations_controller.rb
16: @ages = aaas.at('.age').inner_text.delete('âge').to_i if aaas.at('.age')
17: @names = aaas.at('h1').inner_text if aaas.at('h1')
18: @image_urls = personal_page.at('.main_image img').get_attribute('src') if personal_page.at('.main_image img')
19: @infomation = Infomation.where(name: @names).first_or_initialize
20: @infomation.age = @ages
21: @infomation.image_url = @image_urls
22: @infomation.save
23: byebug
=> 24: end
25: end
(byebug) talen_name
*** NameError Exception: undefined local variable or method `talen_name' for #<InfomationsController:0x00007f0fb5777ce0>
Did you mean? talent_name
nil
(byebug) talent_name
"Yamada Taro"
(byebug)
C'est ça.