Home>

I am making a system that allows you to post images with Ruby on rails.
I want to display an image in the background.

Images posted in active storage can now be saved and displayed.
After that, when I posted to S3 on AWS and tried to save and display the image, an error occurred and
Corrected the HTML description.

I can see that the image data is sent to the record normally,
I am in trouble because the image is not displayed.
I think there is a problem with the code that displays the image in _item.html.erb.

Corresponding source code
app/veiws/veiws/_item.html.erb<code>➡︎ Image is not displayed with this code ➡︎ This code causes an error<% #             <span><% = image_tag'arrow_top.png'%></span>    <ul>      <li>        <% = link_to'Details', item_path (item.id), method:: get%>      </li>    <% if user_signed_in?&&current_user.id == item.user_id%>      <li>        <% = link_to'Delete', item_path (item.id), method :: delete%>      </li>        <% # Prefix argument%>        <% # Item ID (item.id)%>        <% # HTTP method is an HTTP method called DELETE%>        <% # method option with symbol: delete%>      <li>        <% = link_to'edit', edit_item_path (item.id), method:: get%>        <% # get Returns information from the server to the browser. It is used to obtain information, such as when simply browsing a website. %>        <% # Check path with Prefix%>      </li>    <% end%></ul>         <p><% = item.text%></p>  <span>      ">      <span>Posted by</span><% = item.user.nickname%>          </span>  >


➡︎ Error details

NoMethodError in Users # show
Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line # 1 raised:
undefined method `url'for # Extracted source (around line # 1):What I tried
<pre><code>app/veiws/veiws/_item.html.erb<code>Contents of the code () after the above correction No image displayed, no error➡︎ When using the first pattern image_url
Sprockets :: Rails :: Helper :: AssetNotFound in Items # index
Showing /Users/uraokayuutarou/projects/nostalgia/app/views/items/_item.html.erb where line # 1 raised:
The asset "" is not present in the asset pipeline.
Extracted source (around line # 1):<% #<% # );"><% # );"><% # ');"><% # An error occurred in the above code.
Error screenshot
https://gyazo.com/f1fe843012299d2dc4ec19060bf82905
Continued
https://gyazo.com/e89d008392120fa0a804ff47f8a90bf3


Supplement

S3 creates a bucket and edits the bucket policy,
I bundle installed gem "aws-sdk-s3", require: false on Gemfie.
Then change config.active_storage.service =: amazon from local to config/environments/delopment.rb.
Next, uncheck "Block all public access".
Checked New Public Bucket Policy, Any Bucket Policy.
Added to config/storage.yml
amazon:
 service: S3
 access_key_id:<% = ENV ['AWS_ACCESS_KEY_ID']%>secret_access_key:<% = ENV ['AWS_SECRET_ACCESS_KEY']%>region: ap-northeast-1
 bucket: own bucket name
Environmental setting
config/environment/developments.rb
Last description
  # default_url_options [: host] = "localhost: 3000"
  host ='samplehost'
  Rails.application.routes.default_url_options [: host] = host
  # Probably defined in production environment
I was able to describe here and display it locally.
Using the url_for method


Postscript 11/6 20:42
1) I confirmed that I was able to save to S3 with the AWS S3 object on the WEB page.
2) Contents of item_params =>"Pants", "image" =># , @original_filename = "Screenshot 2020-11-06 19.46.55.png", @content_type = "image/png", @ headers = "Content-Disposition: form-data;name = \ "item [image] \";filename = \ "\ xE3 \ x82 \ xB9 \ xE3 \ x82 \ xAF \ xE3 \ x83 \ xAA \ xE3 \ x83 \ xBC \ xE3 \ x83 \ xB3 \ xE3 \ x82 \ xB7 \ xE3 \ x83 \ xA7 \ xE3 \ x83 \ x83 \ xE3 \ x83 \ x88 2020-11-06 19.46.55.png \ "\ r \ nContent-Type: image/png \ r \ n" >, "user_id" =>1} permitted: true>
3) Definition of item model
params.require (: item) .permit (: text,: image) .merge (user_id: current_user.id

  • Answer # 1

    );">
    Here, I wonder if it's not image_tag but image_url or image_path.

Related articles