简体   繁体   中英

UrllinksController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

I have a controller for urllinks and home and have a view for create.js.erb. Showing error when I click on Create Short Link.

Controllers/UrllinksController

class UrllinksController < ApplicationController
    # is looking for instant variables that have been set
    # Using create method as a JS Endpoint 
    # by rendering JS template back to the server
    def create
        shortit = Shortit.new(urllink_params[:original_url])
        @shortit = shortit.generate_short_link

        respond_to do |format|
          format.html 
          format.js #{ render :create }
        end
    end 

    private
    def urllink_params
        params.require(:urllink).permit(:original_url)
    end
end

Controllers/HomeController

class HomeController < ApplicationController
  def index
    # initialise a new instance variable
    @urllink = Urllink.new()
  end
end

Views/Home/index.html.erb

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>

<%= form_with model: @urllink, remote: true do |form| %>
  <%= form.text_field :original_url %>
  <%= form.submit "Create Short Link"%>
<% end %>

Views/urllinks/create.js.erb

<% alert("Hello ,AJAX!"); %> #Tried without <% %>

routes.rb

Rails.application.routes.draw do
  #get 'links/create'
  get 'home/index'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
  post "/urllinks" => 'urllinks#create'
end

javascript/application.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
import Rails from '@rails/ujs'; Rails.start();

spec/request/urllinks_spec.rb

require 'rails_helper'

RSpec.describe "Urllinks", type: :request do
  it "can make link short provided by a user" do
    #getting user input using https and using post request because of using database        
    url = "https://www.amazon.co.uk/testing/12345"

    # For JavaScript End point Set environment requests to text/javascript
    respond_to :text, :javascript

    post "/urllinks", params: { urllink: url }
    urllink = assigns(:urllink)
    expect(urllink.original_url).to eq(url)
    expect(urllink.valid?).to be(true)
    expect(urllink.seven_char_string.length).eq(7)
    #makes sure it saves to the db
    expect(urllink.persisted).to be(true)
    expect(response).to render_template('create')
  end

end

Console: When I click on Click Short Link button

rails s 

Started POST "/urllinks" for 127.0.0.1 at 2022-06-04 11:14:05 +0100
Processing by UrllinksController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "urllink"=>{"original_url"=>"abcde"}, "commit"=>"Create Short Link"}
  Urllink Exists? (0.8ms)  SELECT 1 AS one FROM "urllinks" WHERE "urllinks"."seven_char_string" = ? LIMIT ?  [["seven_char_string", "68e08cf"], ["LIMIT", 1]]
  ↳ app/services/shortit.rb:31:in `block in seven_char_string'
  CACHE Urllink Exists? (0.0ms)  SELECT 1 AS one FROM "urllinks" WHERE "urllinks"."seven_char_string" = ? LIMIT ?  [["seven_char_string", "68e08cf"], ["LIMIT", 1]]
  ↳ app/services/shortit.rb:47:in `generate_short_link'
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/services/shortit.rb:47:in `generate_short_link'
  Urllink Create (1.4ms)  INSERT INTO "urllinks" ("original_url", "seven_char_string", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["original_url", "URI"], ["seven_char_string", "68e08cf"], ["created_at", "2022-06-04 10:14:05.528502"], ["updated_at", "2022-06-04 10:14:05.528502"]]
  ↳ app/services/shortit.rb:47:in `generate_short_link'
  TRANSACTION (3.8ms)  commit transaction
  ↳ app/services/shortit.rb:47:in `generate_short_link'
Completed 406 Not Acceptable in 43ms (ActiveRecord: 6.1ms | Allocations: 5923)


  
ActionController::UnknownFormat (UrllinksController#create is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []):
  
actionpack (7.0.3) lib/action_controller/metal/implicit_render.rb:42:in `default_render'
actionpack (7.0.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (7.0.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (7.0.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (7.0.3) lib/abstract_controller/base.rb:215:in `process_action'
actionpack (7.0.3) lib/action_controller/metal/rendering.rb:53:in `process_action'
actionpack (7.0.3) lib/abstract_controller/callbacks.rb:234:in `block in process_action'
activesupport (7.0.3) lib/active_support/callbacks.rb:118:in `block in run_callbacks'
actiontext (7.0.3) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (7.0.3) lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
activesupport (7.0.3) lib/active_support/callbacks.rb:127:in `instance_exec'
activesupport (7.0.3) lib/active_support/callbacks.rb:127:in `block in run_callbacks'
activesupport (7.0.3) lib/active_support/callbacks.rb:138:in `run_callbacks'
actionpack (7.0.3) lib/abstract_controller/callbacks.rb:233:in `process_action'
actionpack (7.0.3) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (7.0.3) lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
activesupport (7.0.3) lib/active_support/notifications.rb:206:in `block in instrument'
activesupport (7.0.3) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (7.0.3) lib/active_support/notifications.rb:206:in `instrument'
actionpack (7.0.3) lib/action_controller/metal/instrumentation.rb:66:in `process_action'
actionpack (7.0.3) lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
activerecord (7.0.3) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (7.0.3) lib/abstract_controller/base.rb:151:in `process'
actionview (7.0.3) lib/action_view/rendering.rb:39:in `process'
actionpack (7.0.3) lib/action_controller/metal.rb:188:in `dispatch'
actionpack (7.0.3) lib/action_controller/metal.rb:251:in `dispatch'
actionpack (7.0.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (7.0.3) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (7.0.3) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (7.0.3) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (7.0.3) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (7.0.3) lib/action_dispatch/routing/route_set.rb:852:in `call'
rack (2.2.3.1) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3.1) lib/rack/etag.rb:27:in `call'
rack (2.2.3.1) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.3.1) lib/rack/head.rb:12:in `call'
actionpack (7.0.3) lib/action_dispatch/http/permissions_policy.rb:38:in `call'
actionpack (7.0.3) lib/action_dispatch/http/content_security_policy.rb:36:in `call'
rack (2.2.3.1) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3.1) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/cookies.rb:697:in `call'
activerecord (7.0.3) lib/active_record/migration.rb:603:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (7.0.3) lib/active_support/callbacks.rb:99:in `run_callbacks'
actionpack (7.0.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
railties (7.0.3) lib/rails/rack/logger.rb:40:in `call_app'
railties (7.0.3) lib/rails/rack/logger.rb:25:in `block in call'
activesupport (7.0.3) lib/active_support/tagged_logging.rb:114:in `block in tagged'
activesupport (7.0.3) lib/active_support/tagged_logging.rb:38:in `tagged'
activesupport (7.0.3) lib/active_support/tagged_logging.rb:114:in `tagged'
railties (7.0.3) lib/rails/rack/logger.rb:25:in `call'
sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3.1) lib/rack/method_override.rb:24:in `call'
rack (2.2.3.1) lib/rack/runtime.rb:22:in `call'
activesupport (7.0.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/server_timing.rb:20:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/static.rb:23:in `call'
rack (2.2.3.1) lib/rack/sendfile.rb:110:in `call'
actionpack (7.0.3) lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
railties (7.0.3) lib/rails/engine.rb:530:in `call'
puma (5.6.4) lib/puma/configuration.rb:252:in `call'
puma (5.6.4) lib/puma/request.rb:77:in `block in handle_request'
puma (5.6.4) lib/puma/thread_pool.rb:340:in `with_force_shutdown'
puma (5.6.4) lib/puma/request.rb:76:in `handle_request'
puma (5.6.4) lib/puma/server.rb:441:in `process_client'
puma (5.6.4) lib/puma/thread_pool.rb:147:in `block in spawn_thread'

You need a file:

app/views/urllinks/create.html.erb

with content

<h1>Short code</h1>

<p>
  Short code is:
  <%= @urllink.seven_char_string %>
</p>

The controller create method should be changed to:

def create
    shortit = Shortit.new(urllink_params[:original_url])
    @urllink = shortit.generate_short_link

    respond_to do |format|
      format.html
    end
end

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