Getting parameters of one view in another - ruby-on-rails

I am completely new to RoR. I have a parameter in one view of a controller now how can i get it in another view of the same controller?

You can set up a form or a link_to in form-a and make a HTTP (POST/GET) to form-b. Remember that you can put also any HTML (or JS) in your view that you like.
Have a look at:
link_to
link_to "Profile", :controller => "profiles", :action => "show", :id => #profile
# => Profile
If you want to link to a associated table you can easy do this with:
# #person.company.name is the name of the link
# company_path(#person.company) is the associated model
<%= link_to #person.company.name, company_path(#person.company) %>

I approve you to read the ruby on rails guides
Read the getting started chapter and then create the blog application you will see
that gives you a great entrace to ruby on rails.
Try to understand the Rails way.

Related

Action Controller error. Url Generation error. No route matches

I'm working through the "Ruby on rails 3 essential training" on lynda.com and am having an issue while generating my server. So far I have a subjects_controller.rb, linked to my views folder, to the file list.html.erb. My error when trying to start the server is:
No route matches {:action=>"show", :controller="subjects", :id=>1}
In my list.html.erb file I have written the code:
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", '#', :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
My subjects_controller.rb looks like:
class SubjectsController < ApplicationController
def list
#subjects = Subject.order("subjects.position ASC")
end
end
I have double checked to make sure I have everything written the same as the instructor but there seems to be a missing link. Any ideas? If I totally cut out the action:
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
Then the server starts up. There must be a problem here but I'm not sure what it is. Also when the instructor inputs link_to on his text editor, the txt turns a different color and mine does not. Same thing with his "#" instance variable. Mine doesn't change color. Not sure if this means anything either. Thanks for any input!
Here is my config/routes.rb file:
Rails.application.routes.draw do
root :to => "demo#index"
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
get 'subjects/list'
end
Short version: The error message is telling you exactly what is wrong. You have no route that matches, because while your action is named list, your link specifies :action => 'show'.
Longer version: The second argument to the link_to helper is supposed to tell Rails what URL to generate for the link, usually by specifying one of your routes by name, but sometimes (as in this case), by specifying the action (and optionally the controller). You're specifying the action show. The subjects controller is implied. Therefore, Rails is trying to find a route (in the ones defined in your routes.rb) that GETs the SubjectsController#show action. However, as you can see from your routes.rb, you only define one route on the SubjectsController, and that's list.
If you're ever confused about what routes you have or what their names are, you can use the rake routes task to list them all out in a nice readable format.
Edit to respond to followup question:
The instructor is telling me that when you generate a controller and
action that its supposed to add a route to the routes.rb folder. This
worked for me earlier but when creating these actions that I'm having
trouble with now, it didn't generate anything in the routes.rb folder.
Do you know why that is?
When your instructor says 'generate', they probably mean 'use the rails generate command'. When you use the generator to create a controller and specify the actions in it, the it will also add those actions to the routes file.
If, on the other hand, you write the action into an existing controller (including using the generator for the controller but not specifying actions), or create the controller file yourself, you'll have to update the routes file manually. If you are using the generator and specifying actions and aren't getting updated routes, I'm not sure what's going on.
Personally, I prefer to write my routes by hand anyway - the generator often doesn't get them exactly right.

Pass data safely from view to controller in rails

I want to pass data from a view (link) to a controller so it can look up the related information. Services for a company, in this case.
I see examples where people have added to params like this:
<div>
<%= link_to 'Services', :controller => 'company', :action => 'services', :company_id => #company.id %>
</div>
...but that results in a transparent (unsafe) URL like this:
http://localhost:5000/company/services?company_id=17
Is there a way to get around this without stuffing data into the Session? What's the best practice on links inside an app that requires authentication?
THere is no such major harm in passing data like this in View.
Still if you insist on having, then check prettyurls:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Prior to we must have valid checks in controller & model files.
1. Valid Checks and redirection in Controller is helpful.
2. Depending on need adding validations in model can be a good support.
<%= link_to "Sign in", new_session_path(:id => Base64.encode64("1")) %>
and in your controller
def new
id=Base64.decode64(params[:id].to_s)
end
this is another form for create a link with data
check your routes with command un console rake routes
for more information read this documention
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Move one page to another in Ruby on rails?

In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . As i set root"home#page" as my root page. Now i want to transfer from "page.html.erb" into "index.html.erb" . How can i do that. And the code i written is below.Do i have to enter some thing in my controller class. please suggest.
these are the links that i tried. (How to create an anchor and redirect to this specific anchor in Ruby on Rails)
<a rel="nofollow" href="index.html.erb">Transfer to index</a>
You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller.
I strongly encourage you to review the ruby on rails MVC principles. You can read about routing and linking aswell.
Responding to your question, check out the command "rake routes". It will list the defined routes of your app and helps you to use them.
Try to replace your code by this:
<%= link_to 'welcome', welcome_path %>
<%= link_to "Link", controller:"controllername" %>
is the code you should use
You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view.
In routes.rb:
match '/welcome' => 'welcome#index', :as => :welcome
In your page.html.erb view:
<%= link_to 'Go to Welcome Page', welcome_path %>
If you want go for index action of Welcome controller then you can use:
<%= link_to "Transfer to index", welcome_path %>
Check the rake routes for path.
Plese refer link

Confused about following step from Ruby on Rails Guide website

I am a totally new to learn rails and just start this Ruby on Rails Guide website.
And, i am a little confused when I following some step.
1.Why "redirect_to #post" will get "show action"?
2.Why we need use "index action" to list all posts?
3.When we use "<%= link_to "My Blog", controller: "posts" %>", I know it will link to posts controller, but don't know why will display index action which list all posts?
Thanks.
"show" action is to display a object detailed information, and the object is #post.
"index" action is to display one sort of resources.
You can use the default action, but you can customize it at well, like below:
.<%= link_to "Blog", {:action => "any_action", :controller
"posts_or_any_controller"}, {:method => :get} %>

Rails ignoring render and redirect_to

I've got a really simple rails question here but I can't seem to find the answer anywhere. I guess some of the problems stem from me following a tutorial for Rails 1.2 with Rails 2.1. Anyway..
I'm writing a blog system and I'm implementing the comments bit. I have comments displaying fine once I've created them using script/console, but getting the comment form itself working is the hard bit.
In posts_controller.rb I have
def comment
Post.find(params[:id]).comments.create(params[:comment])
flash[:notice] = "Added comment"
#render :action => show
redirect_to :action => show
end
and in show.html.erb (the view) I have
<%= form_tag :action => "comment", :id => #post %>
<%= text_area "comment", "body" %><br>
<%= submit_tag "Post Comment" %>
When I submit the form it tries to go to the urb /posts/comment/1 which is obviously incorrect, and it complains that it can't find a template. Obviously I don't want a template there because I've told it to redirect to the show action because I want it to just re-display the post's show page, with the new comment there.
I've tried both the commented out line (render :action => show) and the redirect_to line, and neither seem to do anything at all.
I'm sure I'm missing something simple, but what is it?
Does redirect_to :action => 'show', :id => params[:id] with quotes around show work?
Rails 2.1 embraces "RESTful resources". show just happens to be the name of one of the predefined REST actions that all rails controllers use.
Rails does some magic behind the scenes, and :show is equivalent to "display this one specific element with a specific given ID". Sounds like it's getting mixed up with that. The ID is probably defaulting to "1". Hence the generated URL you're seeing from the render call
The Rails 2.1 way of doing it would use the following actions and templates:
index - displays the full list of comments
create - add a new comment
show - display a specific comment only (not the full list). Doesn't sound like this is what you want, but the "magic" inside rails will default to this.
There are also actions for new (show view to enter a new comment) edit (show view to do an edit of an existing comment) update (handle update submission) and destroy (duh), but it doesn't look like you'd use them in this example.
Do you have a link to the tutorial? Wouldn't be too hard to port it to Rails 2.1 style.
yes, you use old rails style.
Something new:
form_for :comment, :url => { :post_id => #post } do |f|
f.text_area :body
submit_tag "Post"
end
you can use resources for posts and comments, search google for better tutorial or install rails 1.2.6:
gem install -v 1.2.6 rails

Resources