jeudi 7 mai 2020

How to use form_tag with "get" method

I'm trying to use form_tag with get method but it become a post method

<%= form_tag(:method => "get", :room => @room.id) do %>
  <%= label_tag("Vote for:") %>
  ..some thing else..
  <%= submit_tag("Vote") %>
<% end %>

this is my code but if i inspect elemen, it was become :

<form accept-charset="UTF-8" action="/start/testing?method=get&amp;room=43" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="j4fgdRYJXeyQfmnqMQNFb2fYuw2+/UXRw1UFxc0WuHo=" /></div>
  <label for="Vote_for:">Vote for:</label>
  ..some thing else..
  <input name="commit" type="submit" value="Vote" />
</form>

I think i become a parameter, how can I fix it ? Thank you

mercredi 6 mai 2020

Rails 4 Sprockets : No such file

I'm currently in the middle of upgrading a Ruby on Rails 3 project to Ruby on Rails 4.0. Several of my tests related to controllers are failing because specific files cannot be found when rendering the templates during the tests:

ActionView::Template::Error: No such file or directory

All these errors have the same in common, that they try to load files from the tmp/cache in the Rails application. This was working all fine under Rails 3. Is something specific required for asset compilation under Rails 4 in tests?

# config/application.rb
# Enable the asset pipeline
config.assets.enabled = true

The above is not overwritten in our test environment, so I assume this remains enabled? And we used Sprockets in Rails 3 as well.

  • Rails : 4.0
  • Sprockets : 3.6

I'm probably missing something obvious, just can't find it...

Error output

test_0001_should show a form to upload attachments and display existing ones(AttachmentsControllerTest::#new):
ActionView::Template::Error: No such file or directory @ rb_sysopen - /root/rails_backend/tmp/cache/assets/test/sprockets/v3.0/m7/m7YIr0duEvKhRldOP5LjpABJF7kd9H6aT5dCMyGnOl8.cache.47262198725000.3291.4516
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:278:in `initialize'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:278:in `open'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:278:in `atomic_write'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cache/file_store.rb:107:in `set'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cache.rb:212:in `set'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cache.rb:86:in `fetch'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:56:in `file_digest'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/unloaded_asset.rb:104:in `dependency_history_key'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:304:in `fetch_asset_from_dependency_cache'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `yield'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/environment.rb:30:in `find_asset'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:92:in `[]'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-2.3.3/lib/sprockets/rails/helper.rb:123:in `asset_digest_path'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-2.3.3/lib/sprockets/rails/helper.rb:76:in `compute_asset_path'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/asset_url_helper.rb:132:in `asset_path'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-2.3.3/lib/sprockets/rails/helper.rb:91:in `asset_path'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/asset_url_helper.rb:234:in `javascript_path'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/asset_tag_helper.rb:58:in `block in javascript_include_tag'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/asset_tag_helper.rb:56:in `map'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/asset_tag_helper.rb:56:in `javascript_include_tag'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/sprockets-rails-2.3.3/lib/sprockets/rails/helper.rb:148:in `javascript_include_tag'
    /root/rails_backend/app/views/layouts/attachments.html.erb:2:in `block in _app_views_layouts_attachments_html_erb___457563631613762413_47262336759680'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/haml-4.0.7/lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/capture_helper.rb:38:in `capture'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/haml-4.0.7/lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/helpers/capture_helper.rb:152:in `content_for'
    /root/rails_backend/app/views/layouts/attachments.html.erb:1:in `_app_views_layouts_attachments_html_erb___457563631613762413_47262336759680'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/template.rb:143:in `block in render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications.rb:159:in `block in instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications.rb:159:in `instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/template.rb:141:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/renderer/template_renderer.rb:47:in `render_template'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/renderer/template_renderer.rb:17:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/renderer/renderer.rb:42:in `render_template'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_view/renderer/renderer.rb:23:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/rendering.rb:127:in `_render_template'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/streaming.rb:219:in `_render_template'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/rendering.rb:120:in `render_to_body'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/rendering.rb:33:in `render_to_body'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/renderers.rb:26:in `render_to_body'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/rendering.rb:97:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/rendering.rb:16:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
    /usr/local/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/core_ext/benchmark.rb:12:in `ms'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:41:in `block in render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.13/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:40:in `render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/implicit_render.rb:5:in `send_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/base.rb:189:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/rendering.rb:10:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:453:in `_run__64868443609262235__process_action__callbacks'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/callbacks.rb:80:in `run_callbacks'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/callbacks.rb:17:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/rescue.rb:29:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications.rb:159:in `block in instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/notifications.rb:159:in `instrument'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.13/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/base.rb:136:in `process'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/abstract_controller/rendering.rb:44:in `process'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/test_case.rb:572:in `process'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/test_case.rb:64:in `process'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/active_model_serializers-0.9.5/lib/action_controller/serialization_test_case.rb:25:in `process'
    /root/rails_backend/vendor/bundle/ruby/2.2.0/gems/actionpack-4.0.13/lib/action_controller/test_case.rb:472:in `get'
    /root/rails_backend/test/functional/attachments_controller_test.rb:78:in `block (2 levels) in <class:AttachmentsControllerTest>'

Page reload on a form submit! Rails API backend Javascript frontend

I'm having trouble figuring out my javascript. The e.preventDefault() is not working. I've tried changing the submit input to a button as well. I know with a form and using rails that it has an automatic rage reload but I thought e.preventDefault was suppose to stop that. Is there some hidden feature in the backend that I need to turn off? I set my project up to be an api by using an api flag. It also has all the right info for cors. My server is showing my data correctly ...it's just the frontend I cant get up.

I'm going to post a sample code I followed.


<html lang="en" dir="ltr">

  <head>

    <title>Problems</title>

    <meta charset="utf-8">
    <link rel="stylesheet" href="styles.css">
    <script type="application/javascript" src="src/user.js" charset="UTF-8"></script>
    <script type="application/javascript" src="src/problem.js" charset="UTF-8"></script>

  </head>

  <body>

   <div class="container" id="container">

    <h1>Everyone Has Problems</h1>

        <div id="new-user-and-new-problem-container">
            <form id="new-user-form">
                <label>Your name:</label>
                <input type="text" id="new-user-body"/>
                <input type="submit"/>
            </form>
        </div>

    </div>

    <div id="problems-container" class="problems-container">
    </div>

  </body>

</html>```

src/user.js
```document.addEventListener('DOMContentLoaded', function(){
    User.createUser()
})

class User {

    constructor(user){
        this.id = user.id
        this.name = user.name
        this.problems = user.problems
    }

    static createUser(){
        let newUserForm = document.getElementById('new-user-form')
        newUserForm.addEventListener('submit', function(e){
            e.preventDefault()
            console.log(e);
                fetch('http://localhost:3000/api/v1/users', {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                        "Accept": "application/json"
                    },
                    body: JSON.stringify(
                        {
                            user: {
                                name: e.target.children[1].value
                            }
                        })
                    })
                        .then(resp =>  {
                            return resp.json()
                        })
                        .then(user => {
                            let newUser = new User(user)
                            newUser.displayUser()
                        })
        })
    }

    displayUser() {
        let body = document.getElementById('container')
        body.innerHTML = ''
        let userGreeting = document.createElement('p')
        userGreeting.setAttribute('data-id', this.id)
        let id = userGreeting.dataset.id
        userGreeting.innerHTML = `<h1>Hey, ${this.name}!</h1>`
        body.append(userGreeting)
        if (this.problems) {
            this.problems.forEach(function(problem){
                let newProblem = new Problem(problem)
                newProblem.appendProblem()
            })
        }
        Problem.newProblemForm(this.id)
    }

}```

src/problem.js
```class Problem {

    constructor(problem){
        this.id = problem.id
        this.name = problem.name
        this.description = problem.description
    }

    static newProblemForm(user_id) {
        let body = document.getElementById('container')
        let form = 
            `
                <form id="new-problem-form">
                    <label>What's your problem?:</label>
                    <input type="text" id="problem-name"/>
                    <label>Describe it:</label>
                    <input type="text" id="problem-description"/>
                    <input type="submit"/>
                    <h4>Your current problems:</h4>
                </form>
            `
        body.insertAdjacentHTML('beforeend', form)
        Problem.postProblem(user_id)
    }

    //is it appropriate for this to be a static method?
    static postProblem(user_id) {
        let newForm = document.getElementById('new-problem-form')
        newForm.addEventListener('submit', function(e){
            e.preventDefault()
            fetch('http://localhost:3000/api/v1/problems', {
                method: "POST",
                headers:{
                    "Content-Type": "application/json",
                    "Accept": "application/json"
                },
                body: JSON.stringify(
                    {
                        problem: {
                            name: e.target.children[1].value,
                            description: e.target.children[3].value,
                            user_id: user_id
                        }
                    }
                )
            })
            .then(resp => resp.json())
            .then(json => {
                let newProblem = new Problem(json)
                newForm.reset()
                newProblem.appendProblem()

            })
        })
    }

    appendProblem(){
        let problems = document.getElementsByClassName('problems-container')
        let li = document.createElement('li')
        li.setAttribute('data-id', this.id)
        li.setAttribute('style', "list-style-type:none")
        li.innerHTML = `${this.name} ~~ ${this.description}`
        let solveForm = `<button type="button" id="${this.id}" class="solve-problem"> Solve </button>`
        li.insertAdjacentHTML('beforeend', solveForm)
        problems[0].append(li)
        let button = document.getElementById(`${this.id}`)
        this.solve(button)
    }

    solve(button){
        button.addEventListener('click', function(e){
            e.preventDefault()
            fetch(`http://localhost:3000/api/v1/problems/${e.target.parentNode.dataset.id}`, {
                    method: "DELETE"
            })
                    e.target.parentElement.remove();
        })
    }

}```

PHP within a ruby on rails application?

I have an unusual question.

I want to display a php file page within my ruby on rails application. So i want to put it in some random directory and under https://ift.tt/3b6cGhd there should be just a independent file.

I don't get how i can do this, since everything has to be connected via routes.

Is this even possible?

mardi 5 mai 2020

undefined method `id' for nil:NilClass in rails

I am creating instagram app from https://medium.com/luanotes/build-instagram-by-ruby-on-rails-part-2-d70b44f5c7e6.

Under this topic: Add a form to create a new Post in Homepage

I am getting this error : undefined methodid' for nil:NilClass`

my index view file

<%= form_for Post.new do |f| %>
  <div class="form-group">
    <%= f.text_field :description %>
  </div>
  <div class="form-group">
    <%= f.file_field :image %>
  </div>
  <div class="form-group">
    <%= f.text_field :user_id,nil, value: current_user.id, class:'d-none'%>
  </div>
  <br>
  <div class="text-center">
    <%= f.submit 'Create Post', class: 'btn btn-primary' %>
  </div>
<% end %>

Controller file:

class PostsController < ApplicationController
    def create
        Post.create(post_params)

        redirect_to root_path
    end

    private

    def post_params
        params.require(:post).permit(:description, :image, :user_id)
    end
end

lundi 4 mai 2020

Rails + Memcachier shows missing key

I am using memcachier in my rails applciation which is running in Heorku, below the gem details

memcachier (0.0.2)

dalli (2.6.2)

Ruby 1.9.3

Rails 3.2.11

Let say I am rendering a file called _credit_account.html.erb and in the file I am using a caching mechanisam to store the user details, code looks like

def credit_account

end

In the partial _credit_account.html.erb

Rails.cache.fetch(
"user_details_of_credit_account_#{account_id}"
) do
UserDetails.for_account(account_id).to_a
end

So in the dashboard of MemCachier I can see the hit for this key

GET OK 5KB user_details_of_credit_account_53625762

But the strange thing is I can see this missed key also for the whole page,

GET Key not found http://sample.app.com/user/credit_account?id=53625762

This looks like I am using the whole page caching but there is no code other than this. Am i missing anything here?

dimanche 3 mai 2020

Ruby: merge three array of hashes in different way

I'm new to Ruby and trying to build a meeting app. I have three arrays containing hashes :

  • one containing my scheduled meetings with dates and therefore an empty array of people
  • one containing the people invited per meeting
  • and a last one containing the people who refused

This materializes as:

meetings = [
 {:id=>"1", :peoples=>[]}
 {:id=>"2", :peoples=>[]}
 {:id=>"3", :peoples=>[]}
]

invited_peoples = [
 {:id=>"1", :peoples=>['Tom', 'Henry', 'Georges', 'Nicolas']}
 {:id=>"2", :peoples=>['Arthur', 'Carl']}
]

absent_peoples = [
 {:id=>"1", :peoples=>['Henry', 'Georges']}
]

And I would like to have : meetings + invited_peoples - absent_peoples like

meetings_with_participants = [
 {:id=>"1", :peoples=>['Tom', 'Nicolas']}
 {:id=>"2", :peoples=>['Arthur', 'Carl']}
 {:id=>"3", :peoples=>[]}
]

I'm looking for a readable solution but I don't find anyone...

Sorry for my english and thank you in advance, Nicolas