I have a problem with calling methods. Here is the code from one of my Controllers:
class MethodsController < ApplicationController
def details
#title = "User Details"
#fname = params[:first_name]
#lname = params[:last_name]
#exam_no = params[:exam_no]
if #fname && #lname then
#candidate = Candidate.create({:first_name => #fname, :last_name => #lname,:exam_number => #exam_no})
grades #This method call doesn't work
end
end
def grades
#title = "Enter Grades"
#candidate = Candidate.find(:last)
#grades = params[:grades]
#candidate.update_attributes({:grades => params[:grades]})
end
end
The details method works fine, clicking the 'submit' button on the 'details' view creates the Candidate and I can 'link_to' the next page.
What I really want to do is: Click 'submit' and be automatically taken to the next view, 'grades' (which is also shown above). I thought a method call to 'grades' in 'details would work, but it doesn't. The 'grades' page title appears, but the view for 'details' still remains.
Is there some way to do this? Possibly call the 'grades' method from 'details' view? I know that's not best practice, but I'm stuck. (I tried #controller.grades but I get a NoMethodError - #controller is nil). I also tried render 'grades' but of course while it renders the view, it doesn't execute the code.
You might be looking for redirect_to instead of render
Redirect_to :action => 'grades'
Related
In a view I call upon a controller method. This however seems to call a different method than I want to do.
The view includes:
<%= link_to("Upgrade account", upgrade_path)
With routes:
get 'signup/organization' => 'organizations#new', as: 'register'
get 'signup/register' => 'organizations#new_premium', as: 'register_premium'
post 'signup/register' => 'organizations#checkout', as: 'signup_checkout'
post 'signup/register' => 'organizations#upgrade', as: 'upgrade'
get 'signup/confirmation' => 'organizations#confirmation'
'Upgrade' does not occur anywhere else in my routes file.
The path in the view should thus call upon the following controller method:
def upgrade
#organization = current_organization
#actioncode = Actioncode.new
#amount = DEFAULT_PRICE
#currency = "EUR"
#description = #organization.id
#transaction_description = "MyDescription"
#transaction_type = "S"
#hash = hash(#description, #amount, #currency, #transaction_type)
render 'checkout'
end
This should render a view (checkout.html.erb) which has two forms. However, instead it re-routes to the root with the message You're already logged in. As it turns out this message originates from the following controller method:
def new_premium
if (logged_in_user?)
flash[:danger] = "You're already logged in"
redirect_to root_url
end
#organization = Organization.new
#member = #organization.members.build
end
I don't see how that method can come into play and why my code isn't working. Does anyone have an idea?
The controller also contains a def checkout but I don't see how that might be of effect. The line render 'checkout' I would expect to render checkout.html.erb and have no relationship with either def checkout or def new_premium... right...?
Look at your routes:
post 'signup/register' => 'organizations#checkout', as: 'signup_checkout'
post 'signup/register' => 'organizations#upgrade', as: 'upgrade'
The same url with the same verb is used twice.
The rule in routing is "first match first served", so I guess they are all handled by checkout.
Fix is simple, change the url for one of them.
This is the first time I've encountered this problem.
I have a view which submits a post request to a controller which updates two tables.
def update
if request.post?
if #circuit
# update
#circuit.update_attributes params[:circuit]
#logical_interface = LogicalInterface.new params[:logical_interface]
#logical_interface.save
#redirect_to :action => 'update', :id => #circuit.id
#success = "Updated." if #circuit.valid?
else
# attempt create
end
end
end
These three lines are what I've added to the controller:
#logical_interface = LogicalInterface.new params[:logical_interface]
#logical_interface.save
redirect_to :action => 'update', :id => #circuit.id # this was added because the view wasn't being updated until refreshed
If I keep the redirect, the view will be updated accordingly but I get no Updated. message in the #success variable.
If I comment out the redirect, the circuit form fields at the top of my form will update but not the table of logical_interfaces that I am adding to but I still get the Updated. success message. Everything is in the view directly, no partials are used.
Hopefully I've explained it properly but if anyone is unsure then I can update the question to go into more detail.
The form is just:
<%= form_tag :controller => "circuit", :action => "update" %>
...
</form>
In the form I use two objects circuit and logical_interface to split up the inputs so that in the controller I can update the circuit and create a new logical_interface.
try to adjust positions of redirect_to and #success, I think redirect_to should be the last line of the block.
And If you use redirect_to, you will lose all your instance variables, so better way is using flash.
in your controller:
flash[:notice] = "Updated." if #circuit.valid?
redirect_to :action => 'update', :id => #circuit.id
in your page:
<p><%= flash[:notice]%></p>
essentially I have a category that you can add comments to, this category shows a lists of tasks. When You add comments you have the ability to reply to said comment, when you do so and hover the reply link you’ll see something much like:
http://localhost:3000/categories/2/category_comments/new?parent=6
We then take that id, pass it to the reply forum and then assign it to the ancestry string in the database to "nest" the reply. The problem is, the parent id is not being passed to the form. The form's hidden field is blank. Why? We can walk the path this id should take in the following code.
categories_controller
def show
#category = Category.find(params[:id])
#category_comment = #category.category_comments.build
end
This shows the comment on the category page, and passes the parent_id of the comment your replying to, to the form.
When we click reply, we trigger the category_comments#new and #create methods shown below.
category_comments_controller
def new
#category = Category.find(params[:category_id])
#category_comment = #category.category_comments.build(:parent_id => params[:parent_id])
end
def create
#category = Category.find(params[:category_id])
#category_comment = #category.category_comments.create(params[:category_comment].merge(:user_id => current_user.id))
if #category_comment.save
redirect_to project_category_path(#category.project, #category), :flash => {:success => 'Created comment'}
else
redirect_to :back, :flash => {:error => 'Could not create comment'}
end
end
update:
this is no longer a form issue it is a controller issue, dealing with passing the parent_id to the form.
Try this:
<%= link_to 'Reply', new_category_category_comment_path(#category.id, :parent_id => category_comment.id)%>
Do you have has_ancestry defined in your model? I think not having it there would be a valid explanation for this not working.
Some how this magically fixed its self. I am not sure how or what happened, but it magically works now >.>
This seems like a fairly simple problem to me but I have been having some issues.
In one of my views I use something like
<% if current_page?(:controller => "activities", :action => "new") %>
*Do something here*
<% end %>
and it does something specific on the new page for a form. Easy enough and it works great.
Unfortunately, I've found that when you have a "new activity" form (assume normal scaffolding controller), the url will go from
http://localhost:3000/activities/new
after submitting an error prone form to
http://localhost:3000/activities
but it will still show the new activity form with the respective errors. So basically everything works how it is supposed to EXCEPT that I need the url to be http://localhost:3000/activities/new for the current_page? function to recognize that it is indeed a new form page.
I'm wondering if there is some kind of work around to this issue. Thanks!
OH and here is the controller code, in case anybody needs to see it
Controller Code
def new
#activity = Activity.new
end
def create
#activity = Activity.new(params[:activity])
if #activity.save
flash[:notice] = "Successfully created activity."
redirect_to #activity
else
render :action => 'new'
end
end
Think you will need to check for create as well as new
<% if current_page?(:controller => "activities", :action => "new") or current_page?(:controller => "activities", :action => "create") %>
not so pretty maybe wrap it up in a helper method?
You could also check if the created at field is blank. As it won't be set till the activity is created.
Suppose this is my users controller:-
class UsersController < ApplicationController
def show
#user = session[:user]
end
def prepare
session[:user]= User.find(:first)
redirect_to :action => 'show'
end
def update
#user = session[:user]
#user.name = 'rai'
redirect_to :action => 'show'
end
end
View for show.html.erb
<%= #user.name %>
Show page
<%= link_to 'Update', :action=> 'update' %>
Now Explaining the issue:---
Suppose first time user opens the browser with
http://localhost:3000/users/prepare
o/p will be:---
Mohit Show page Update // supposing user table has values mohit as name
Now when he click on update he will get as output like this:--
rai Show page Update
But this should not happen cause
firstly when are at prepare action where value is fecthced from db and its mohit. and then he is redirected to show ie displying the values from session. ie mohit
Now when user click on the update he is redirected to update when value from session is stored to a user instance and the name attribute of that user instance has been modified to rai. and finally redirected to show page.
Now in this page when user's name is displayed its showing rai.. thats the QUESTION why??
cause session should store the same mohit value cause we havnt made any change in session..
When you are doing
#user = session[:user]
#user variabe is assigned reference to the object session[:user], not the copy of it.
So when you are modifying #user, session[:user] is also modified, as they are essentially the same object.
I'm not sure, but I think it is something with hashes and classes and about copying them. So when you do:
#user = session[:user]
You are not making a copy of object but it is something likre reference in C++, both #user and session[:user] are reffering to the same object, so when you modify one, you get both modified.
Example from console:
a = {}
a[:user] = User.first
a[:user].firstname # => "Mohit"
b = a[:user]
b.firstname = 'rai'
a[:user].firstname # => 'rai'
a[:user] = User.first
a[:user].firstname # => 'Mohit'