How to get "http://localhost:3000/users/current" from user_path(:current)? - ruby-on-rails

I tried to practice #163 Self-Referential Association using Rails 3.2.21 and work fine almost, but I can only display
http://localhost:3000/users/1
rather than
http://localhost:3000/users/current
in URL address bar. I read and search almost all code, I only find one current:
<%= link_to "View Profile", user_path(:current) %>.
What should I do for this? It is relative to Rails 3? Thanks a lot!

The Railscast is a little unconventional, but the user_path() method, will take in a value (object, string, int, symbol) and call to_param on it. A symbol (in this case :current) will turn into "current" and the url built will be "/users/current". If you pass it a #user (User instance), the to_param method will return a "1" (the id of the object) giving you "/users/1".
I say this code is unconventional because the users_controller#show method doesn't use the ID to find the #user. It just sets it to current_user for convenience. Ryan was not trying to teach this necessarily, more about the restful friendship controller and data-modeling friendships.

Try this
<%= link_to "View Profile", users_path(:current) %>.

Related

Injecting values into all link_to method calls in Ruby on Rails

I have a strange requirement that I need to inject a value at the beginning of all local links. This is a legacy app and is quite large so I'm looking to do it under the hood, maybe with a monkey patch.
Basically if I have a link_to "Go to dashboard", dashboard_path or link_to "Create a new Job", new_job_path that they would both generate links that look like "/some_value/dashboard" and "/some_value/jobs/new"
Tried a few things and they all have failed. Any ideas?
You can try something like this in your helper instead of monkey patching link_to.
def custom_link_to(link, url, opts={})
url = "append_here/"+url
link_to(link, url, opts)
end
Now you can call this action instead of calling link_to wherever applicable and also use link_to in case if you don't want to override.
custom_link_to("Go to Dashboard",dashboard_path,{})
UPDATE
In case of overriding everything, something similar to this might be helpful - Monkey patching Rails

Checking Data from a Controller on Button Click?

I'm new to Ruby, but I have created a quiz-like site where users answer multiple-choice questions. Each question is assigned to a particular quiz, so there could be multiple questions for each quiz. I'm stumped at trying to see whether the choice selected is correct for that answer or not. All the answers were saved with a question_id referrer and a correct boolean.
I have a feeling it has something to do with link_to, but I can't figure it out.
Here is the controller I am accessing:
def check
puts "//////// //////// /////// #{#givenAnswer}"
puts "//////// //////// /////// #{#correctAnswer}"
end
Here is as near as I can get to making it work, but I get a Url error:
<%= link_to "#{#ans.content}", {:controller=>:pages,:action=>:check, :givenAnswer=>#ans.content, :correctAnswer=>params["correct#{#q.id}"]}, :method=>:get, :class => "btn" %>
The above gives a UrlGenerationError.
Both #q and #ans are created by loops in the script. They are defined properly.
Any help would be appreciated. Thanks!
As per syntax given by you for link_to you are missing closing '}' before :method option.
As long as pages controller is not under some namespace then it should generate the correct link and should not give url error.
In check action correctAnswer and givenAnswer will be available in params hash and not directly as #givenAnswer and #correctAnswer. Please see params hash in check action.

is this the right link_to rails syntax?

<%= link_to "Profile", #user %>
# => Profile
if i use the above code replacing "Profile" with "Category" and #user with #category/#subcategory what do I then point the html link a href etc to?
Check the documentation for this method here.
The first parameter of the link_to method is the displayed text.
Secondly, you can pass in a single instance of an object which will generate a link to the objects #show action.
You may however pass a link explicitly (most common in my opinion).
This can be done by using the Rails path-helpers (user_path(#user)) or by passing in a string.
In your example, if you exchange #user with #category it would link to the categories #show action instead (Assuming you have a Category model and #category isn't nil.
Again, have a look at the documentation of the link_to method and get familiar with it.

New to Rails: How to pass arguments from a textbox to another controller?

I am new to Rails and don't quite understand what I'm supposed to do. Let's say, for example, I want a textbox containing a string to be passed into another controller (another page?) when the user clicks a button. How would I go about doing that?
Functions of controllers are pages, correct? Can a function take parameters just like a normal method? (E.g. sum(x,y))
For complete information, check out Rails Form helpers. Basically, you give the form_tag method a path which points to the controller and the action that you want to handle the form submission. For example,
<%= form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
Here, the action and controller that search_path points to (defined in your routes) will receive the form submission and the value from the text field.
Your action in the controller IS a function, but it will not receive the value from the form submission as a parameter to the function. Instead, you will access it through the params hash. In the example above, you can access the value from the text field as
params[:q]
What are you doing with the string? Storing it? Using it as a parameter on another page?
I suggest you take a look at the Getting Started Guide, go through it, and pay particular attention to the What is Rails? section, where it explains MVC architecture and REST (Representational State Transfer.)
There are dozens of other Rails tutuorials out there, I'm sure if you searched this site you'd find many questions like this one:
https://stackoverflow.com/questions/2794297/how-to-learn-ruby-on-rails-as-a-complete-programming-beginner
Functions of controllers are pages, correct? Can a function take parameters just like a normal method?
Functions of controllers are pages if that's the route you've set up in your routes.rb configuration file. I suggest you run through some tutorials to understand what Rails is for and how it works.

Hard coding routes in Rails

Let's say I have this:
<%= link_to "My Big Link", page_path(:id => 4) %>
And in my page.rb I want to show urls by their permalink so I use the standard:
def to_param
"#{id}-#{title.parameterize}"
end
Now when I click "My Big Link" it takes me to the correct page, but the url in the address bar does not display the desired permalink. Instead it just shows the standard:
wwww.mysite.com/pages/4
Is this because I hard-coded an id into the page_path? It also does not work if I use straight html like..
My Big Link
I would appreciate it if anyone could verify this same behavior and let me know if this intended or not. I need the ability to hard code :id's to specify exact pages...
Just use page_path(page). I guess the path helpers don't access the database themself (which is good), but if they are being supplied with an object and that object has a to_param method this method is being used to generate an identifier.
<%= link_to "My Big Link", page_path(page) %>
It's because you are specifying the id:
page_path(:id => 4)
You could specify the path you want in this method:
page_path(:id => "#{id}-#{title.parameterize}")
Where have you defined the to_param method? In the model?
UPDATE TO MY QUESTION ---------------------->
Thanks all for the answers. This was kind of a one off situation. My solution was to simply go with html:
My Big Link
Which produced the desired:
wwww.mysite.com/pages/4-great-title-here
I didn't want to loop through page objects and waste a call to the database for this one link. Much appreciated for all the answers though!

Resources