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 5 years ago.
Improve this question
I'm diving into Ruby on Rails and I'm trying to get a grasp on the available docs for the API. I'm trying to find out what the list of "options" are for the redirect_to method in the ActionView API. In the RoR API docs, it says...
redirect_to(options = {}, response_status = {})
but it doesn't list what the available options are.
What are the available options for the redirect_to method?
Just so I don't have to ask more questions, where can I find this list of options in the API docs?
As the doc says:
Redirects the browser to the target
specified in options. This parameter
can take one of three forms:
Hash,
Record,
String starting with protocol:// (like http://),
String not containing a protocol,
:back.
Example:
redirect_to :action => "your_action_name"
or
redirect_to post_url(#post), :status => :found
UPDATE 1: (after the downvote ;) )
Its not about specific keyworks, its its "types" of data passed in the option. As you said, what about "id" or "action", they are a part of a hash. see 1.
Hash - redirect_to :action => "show", :id => 5
Record - post
String starting with protocol - "http://google.com"
String not containing a protocol - /images/foo.jpg
:back
So, these are the 5 "types" of values which can be passed in redirect_to, it was again a copy from ruby docs because that is what is stands for. I cannot be more clear than that.
UPDATE 2
Let's take an example: Say, you have ordered an item and after you make the payment, you should be redirected back to that item's page which you have just ordered.
Code_for_placing_the_order
redirect_to :action => "show_item", :id => 5, :current_user => "john"
So, when you do this, the action, "show_item" will be called, which takes in id as an lookup value for the item and the current logged in user current_user.
So, as on a form submit, you call an action and pass the form values, you do the same in redirect.
UPDATE 3
The example I gave was of a hash. So, a hash is created which is passed over the URL to the redirected page.
key | value
-------------------------
action | show_item
id | 5
current_user | john
Now, this hash is forwarded to the next page. The values of action and id should be same and then you can add ANY symbol you want to. They will just be forwarded with it's respective value where you can read them in your show_item action method.
PS: I am learning rails, suggestions are welcomed :)
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Currently i am learning rails so i'm sorry for my bad knowledge and probably for a silly question. :)
I created a simple index page in a controller called pages.
Inside index.html.erb i have a form where a user can sign up for a newsletter which is stored in #newsletter and i also have a variable called #title where title of the page is stored in database.
I want to create an administration panel available only for the owner of the website where he can access and display all newsletters, also i would like the owner to have the possibility to change the title.
Can i achieve that if i create another controller ?
What do you recommend, to keep the same controller for admin and pages or create a new controller for admin area ?
Also, one more question, when do i need to create a model? Everytime when i need a new table in database, isn't ?
I should have a single model for every controller or i can have more models and one controller?
Is there any connection between pages and the newsletter?
If newsletter is not in a relationship with pages, then you can place the newsletter form in a partial and render it where you want.
This a possible db schema for you:
'Page.rb' model with the fields that you need like: title:string, slug:string (i prefer friendly_id), body:text...
'NewsletterEmail.rb' model with the fields that you need: email_address:string, name:string (optional), subscription_status:boolean (to track un-subscription)...
Every model needs a controller for you to track in admin.
For the front-end, you must setup a route like 'match: "/:slug" => "public#page"', where you can catch your page like #page = Page.friendly.find(:slug) and then assign the #title variable #title = #page.title.
Hope this helps you.
For your admin controllers pages_controller.rb and newsletter_emails_controller.rb you can use a before_action :check_admin_rights and define in application_controller.rb
def check_admin_rights
authenticate_or_request_with_http_basic('Administration') do |username, password|
username == 'admin' && password == 'password'
end
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's what I need to achieve :
A user visit the login page. Upon the first visit, the browser will record his IP and data from a cookie (cookies[:user]) and send it to the server. The server then check whether the IP or the user is in the block list and if the user is in the block list, it will automatically redirect the use to a blocked page!
Can we do this in Rails and how?
This could be done quite easily, take this for example
def new
if cookies[:userid].present?
if User.find(cookies[:userid]).userblocked == true
redirect_to "/block_page"
end
elsif #failedtries == max_tries
User.find(cookies[:userid]).userblocked = true
cookies[:userid] = #id_entered
end
end
Where 'userblocked' is a true or false value in your database. This method is full proof against people who can freely change their IP. However if your heart is set on using IP's then this is an example:
def new
if cookies[:blocked].present?
redirect_to "/block_page"
else
if BlockedIps.find_by_ip(request.remote_ip).blocked == true
redirect_to "/block_page"
elsif #failedtries == max_tries
BlockedIps.find_by_ip(request.remote_ip).blocked = true
cookies[:blocked] = true
end
end
end
Something like this could be what you are after. Give it a try, I haven't tested the code. This is just from my head at this moment in time, but it should do what you want, providing you have some sort of system to store the IP in a database when the max tries have reached.
Explanation:
The way it works is that it checks to see if the blocked cookie is present, if so it redirects to the block page, if not it checks to see if the users IP address is in the block list of IP's. If the IP is present and the block column is set to true then the user will be redirected to the block page, else the system checks how many tries they have made and if it is equal to the max tries it adds the users IP to the database and creates a cookie on their PC which can be checked on next visit.
You can make a method for checking the IP address and authenticating then use before_action :yourmethod on top of your controllers. before_action runs before any other action in your controller so it is a good way to authenticate before showing any views.
You can use Conner Stephen McCabe solution above or take a gem for this purpose.
User can easily clear the cookie, so it's not safe anyway.
Example: https://github.com/kickstarter/rack-attack
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 new to rails and still learning the ropes via railstutorial, but the book does all changes to the db via form submissions (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html). But how would I go about submitting (updating the db) without this, lets say I want to add some score which is computed on page ,(for example via a js counter - for simplicity lets just say its a constant 10) and my db consists of a column called score. Then after pressing a submit button how would I go about updating the db?
Thanks
Two trivial ways:
Use a form
Use a URL parameter with a link
The processing (other than GET v. POST) on the Rails side is identical--it's just a parameter.
If you're using JavaScript, there's not necessarily a reason to not use a form, though, since you could just update a form input element and submit normally.
It is quite simple, actually.
The constant 10 is submitted from the view. The submit needs to point to the controller action that will handle this request. So the submit button should build the url using :controller, :action, :id parameters. In a form, this is handled in the form_for declaration. You will deal with in the button_tag declaration.
The routes should be configured so that this message can reach the controller/ action.
The constant 10 is transported in the params hash. If the field is my_counter, then look for params[:my_counter]. If the form had been for a model, say tweets, then it might be in params[:tweet][:my_counter].
In the controller action, possibly update, you will first fetch the record to change with something like #score = Score.find(:params[:id]). This params[:id] is also coming from the view with the submit. Change the counter here, and save.
def update
#score = Score.find(:params[:id])
#score.counter = params[:my_counter]
#score.save
redirect_to :action => :index # or wherever
end
Good luck.