Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've noticed, that "index" action of my controller is called twice.
The action has following structure:
def index
if params[:tags].nil?
# [fork #1] just return the whole collection of model
#items = Item.all
else
# [fork #2] filter items by tags
#items = Item.select_by_tags params[:tags]
end
# Another operations with #items
# The result will be incorrect, because, when tags in params are specified,
# controller action will be first executed for fork #2, and then for fork #1.
# In view, i get #items from fork #2 and result of THIS piece of code for fork #1
end
On the page i have links with URLs, like "/items?tags=tag1, tag2", clicking on them i get "index" action, called twice
I have no idea, why this happens ...
I've figured it out.
The reason of such a behavior was a JavaScript code, being called after page load. This code implements deep-linking for some parts of the page.
Be careful about this.
Thanks.
I faced similar issue and in our case it was an offending pjax code with 6s of timeout resulting in one extra request (i.e. one extra regular HTML request) if pjax didn't complete in 6s.
Another thing to check is whether you have PostCSS listed as a dependency in your package.json. If you do, and you don't have a postcss.config.json file, fix that. The most basic file appears to be
module.exports = {
plugins: []
}
Restart your server, reload your page, and check the server log. There should be only one request being reported as processed.
Very late to the party, but hope this helps someone. I was temporarily stumped in a trivial Rails 6.1.4.1 app seeing the log.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 20 days ago.
Improve this question
i'm having trouble with my rails aplication.
i have a form who should submit some fields to be search to /cities/search/<params_here>.
but, when i submit form, the routing goes to wrong action, and perform a set_city funcion... (??? why this happens ??? )
if the route exists and be declared before the other routes generates from :resources, that should't work?
It seems that there are two problems with the above code.
1.The URL in the form_with:
The URL ideally should be url: "cities/search",.
Since you are using form_with, the values will be available in the form of query params.
This is where our 2nd change comes in.
2.The route that you've set:
It should be get '/cities/search', to: 'cities#search', and as mentioned above, the form fields and their values will be available in the query params.
In the cities_controller's search action, you'd get the params by using params[:query] and params[:search].
Please check form helpers once so that you get a clear idea of it's working.
I have not tested this, so let me know if this helps and if there are any other issues after the above changes.
Also, it is good practice to post the code in the questions in text format rather than images. Ref. this link to understand why.
You can use a collection route without needing to think about route priority:
resources :cities do
get 'search', on: :collection
end
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
My Rails application frequently receives bogus traffic from hackers scanning for vulnerabilities, hitting URLs like /vb/showthread.php%3C/a. These show up as noise in our logs and I would like to filter them out or handle these in some way (such as alerting someone to the scanning attempts).
Is there a Rails or Rack gem that already does this, or are there similar libraries in other frameworks that do the same thing?
The rack-attack middleware gem is general enough to block requests based on any attribute of a request, and provides other advanced features for handling malicious traffic, such as request throttling, blocklists, and request logging.
I'm not sure if this really answer your question by I think a custom filter will be better instead of a general gem.
I've added a before filter on my applicaton_controller to handle weird request, then you can alert and do what you want.
class ApplicationController < ....
before_action :filter_hacks #first filter
private
def filter_hacks
if request.format.to_s == 'php'
#do something: email, save report, anything
head(:ok) and return false
elsif #add other filters
end
end
I'm sure it could be improved, but you have full control of what you want to block and what to do when that happens.
This is a great use case for middleware. You can store a list of known bad routes and use whatever logic you'd like to intercept and handle the request before it makes it through to your app. I would read up more on how to use Rack middleware in the Rails guides.
But it could be accomplished with something similar to this:
class FilterBadRequest
BAD_PATHS = [
"/vb/showthread.php%3C/a"
]
def initialize(app)
#app = app
end
def call(env)
req = Rack::Request.new(env)
bad_request = BAD_PATHS.find { |bad_path| req.fullpath ~= bad_path }
if bad_request
raise ActionController::BadRequest
else
#app.call(env)
end
end
end
This raises an ActionController::BadRequest exception, but you can redirect or do whatever else makes sense within the context of your application. You'll need to make sure to register the middleware within your application config file inside the config block like by calling config.middleware.use FilterBadRequest
I'm using Rails 4.1.8 and I want to get the HTML output, as a string, from a certain controller inside a Helper which I'll later use in a View.
Assuming app were available at this point, this would be what I want. This, however, is just a conceptual example, not working code.
# pseudo-code
# widgets_helper.rb
module WidgetsHelper
def widget_html(widget_id)
app.get("/widgets/#{widget_id.to_s}.html")
app.response.body
end
end
I found this answer which appears to be related but I wasn't sure where to go with it.
I also read about render_to_string but I'm not sure if that's taking me to a workable solution either.
Update
That previous question has a comment that also mentions this question
As a learning experience for Ruby and Rails, I am creating a website for taking polls, stores the results, etc. As part of the polling process, a user has to go through a number of questions and provide answers to those questions. When they are done, they receive a list of recommendations based upon the answers they provided (of type Answer).
I have two parts to my question. One, I think I am heading down the right path. The other, I'm not even sure where to begin, and don't know if it is a good idea.
Here is my Answer model:
class Answer
attr_accessor :question_number, :description, :answer
end
Question 1
I am looking for a way that, when the user submits all the answers (I'm storing their responses in session storage), it goes to my search function - but it is encoded nicely.
Instead of:
http://localhost:3000/results/search?[biglongstringofdifferentanswers]
I would like something like:
http://localhost:3000/results/search/1-answer_2-answer_3-answer
After doing some searching, it seems that what I want to accomplish has to be done with the #parameterize method, but I'm not sure I understand how to do that exactly.
Question 2
The second part to my question is - can I encode my answers so that they aren't directly human readable. I want to do this to prevent people from browsing to each other's answers. For example, the first answer is always the person's unique ID and I don't want to someone to be able to just browse to any old set of results by switching around parameters.
So, I am hoping to get something along the lines of:
http://localhost:3000/results/search/798dh832rhhbe89rbfb289f9234972bdbdbbws3
For this second question, I'm not even sure if this is a good idea, so I'm open to suggestions for this one.
Appreciate any help and guidance on these questions as I continue to explore/learn Ruby and RoR.
If I get it right, there is not any login system and you want submitted answers that you store in your DB to be accessable via url for the user. You said you don't want users to navigate to other users' answers but the user getting the url can still share it.
What I would do is to submit answers via POST method, so you don't have to worry about encoding your params etc. It gets then real easy with Rails.
You can add a public_id column to your answer object that would be a generated big int. After the post methoded submit, once you save the answer in your DB, you could return a redirect to the answer public id url.
something like
def create
answer = Answer.new(params[:answer])
if answer.save
answer.generate_public_id # <= would be nice to add if in the answer model 'after_create' filter probably
return redirect_to public_id_answer_path
end
render :partial => 'error'
end
What do you think ?
I'm basically putting together an app that does a super simple question => answer test in an attempt to learn more about rails.
ie:
Question: "What's your dog's name?"
Answer: "Doggington"
I have a Question model with two simple attributes:
question:string
correct_answer:string
My struggle here is how do i apply REST principals to this -- specifically, when i am checking a user's input(answer) to see if he got the question right or not.
I'm not sure if i should do something like modify the "show" method (or any other action) to accept values for answer posted to it... and it SEEMS like I should create a new method in my questions_controller called "verify_answer" or something a long those lines.
This breaks REST.
What do you think?
thanks!
AnswersController#create should accept the answers. Whether or not this controller actually has a related Answer model is irrelevant. One action should never perform two actions. For instance, if your QuestionsController#show both displays the question, and accepts a :put or :post with the answer to the question then you are breaking basic rails design principals.
Note that your routes file might very well look like this:
resources :questions do
resource :answer
end
Which will expose the /questions/8/answer route that you can :post to, which will go to AnswersController#create.
Off the top of my head I'm forgetting the exact name of the helper url method you can use to generate the url. Something like question_answer_path(#my_question).
This routing file is for rails3, which I assume is what you're using since there's no reason to use anything else if you're starting a new app in my opinion :p
If you do have an Answer model (maybe you want to store users' answers and look at them later or aggregate them and come up with statistics and such) then you should change the router to use resources :answer instead of the singular version.
For more information about routing and some RESTful tips you should visit the Ruby on Rails guide for routing found here: http://guides.rubyonrails.org/routing.html
I'm on an editing spree! There are times when you might need to add an additional method to your Questions controller that isn't strictly REST. This isn't necessarily considered bad practice but just make sure you look at your decisions and find out if you aren't actually just hiding the existence of another resource. I don't consider this to be one of those times as explained above :)