I have the following code in my controller:
class TestController < ApplicationController
##a = 1
def index
#temp = connection.execute("select test_id from mastertest limit #{##a}, 5;")
end
And I have the following code in my View(Html.erb) File:
<button type="submit" value="Next" form="submit_form">NEXT</button>
<form id="submit_form">
<% ##a = ##a + 1 %>
<table>
<% #temp.each do |row| %>
<tr><td><%= row[0] %></td></tr>
<% end %>
</table>
</form>
So basically I am trying to change the value of the class variable ##a on clicking the Next button. But it does not change the value of ##aa. Can someone help me how to do that.
Did you try using helper method?
module ApplicationHelper
##a = 1
def increment_a
##a = ##a + 1
end
end
and in your view just call;
<% increment_a %>
Not that the ## variable is a class variable and it's shared among all instances of the that class. So define that class somewhere in the ApplicationHelper class and then it will be shared and can be accessed in the Controllers and views.
In all cases I highly discourage using class variables in such a way and recommend that you ind another way to share data/variables between view / controller. Maybe use another supporting class or store values in the database.
If you want to alter a Rails variable on a form submission, you should put the code to do it in the action which processes the form.
As you've written it, I believe the variable will get set when the template containing the form is rendered.
I also vaguely recall that there's some special considerations about class variables in Rails apps. You should look into that and make sure you're using a technique that won't cause any unexpected results.
Ok I managed to fix this:
Ruby has something called a global variable which can be declared like this :
$a = 1
Using $a everywhere retains its value in the controller and the view as well.
Related
I am building a Rails project where I have a user, and that user has many Tests (it's like a Trivia game). I have a UsersController where I query the user (going to implement login later).
In my view I have a button which "starts" the test. I need to associate the user with a test, because my User has_many Tests (user_id is a foreign key on tests). My question is, how do I pass my #user object to my TestsController so I can associate the created test with the logged in user?
Here is my UsersController:
class UsersController < ApplicationController
def show
#user = User.find(1)
end
def start_test
redirect_to tests_path
end
end
In my users show view I have:
<p>Welcome <%= #user.name %>!</p>
<%= button_to "Start Test", users_start_test_path %>
When the button is clicked I redirect to tests_path which is in TestsController:
class TestsController < ApplicationController
def index
# here I need to create the Test belonging to the user
end
end
I am new to Rails and don't know how to pass that #user to TestsController so I can create the test belonging to the user. Theoretically I don't even need the whole #user, just the id. Any help would be greatly appreciated.
I think you should improve this by setting the route properly, Instead linking a controller action directly to another controller. I would create the following route.
resources :users do
resources :tests
end
This will force you to always have a user_id. For your new tests path you get this. new_user_test_path(#user)
This will use basics CRUD actions. So to make a new test for the correct user, use the the def new action in the tests controller.
def new
#user = User.find(params[:user_id]
#new_test = #user.tests.new
end
The controllers are standalone. They cann't share variables. You could try to send user id intro the request to controller TestsController (or better use an authentication system). Or directly in the TestsController calls User.find(1) again.
The only variable you can pass between controllers is a parameter through the routing (or the session).
So you can have a HTML Form that passes a body to an action inside a different controller OR for what I see in your case by visiting a route and pass a friendly query string parameter, like an ID.
So you could:
<%= button_to "Start Test", users_start_test_path(user_id: #user.id) %>
And then in your TestController:
class TestsController < ApplicationController
def index
#user = User.find params[:user_id]
end
end
I don't like to pass the current_user variable because it can then be hijacked.
This is how I would set the TestsController
class TestsController < ApplicationController
def index
#tests = current_user.tests
end
def new
#test = current_user.tests.new
end
def create
current_user.tests.create(test_params)
end
def show
#test = current_user.tests.find(params[:id]) # this will only find tests scoped to that user
end
def edit
#test = current_user.tests.find(params[:id]) # this will only find tests scoped to that user
end
def update
#test = current_user.tests.find(params[:id]) # this will only find tests scoped to that user
#test.update(test_params)
end
def test_params
params.require(:test).permit()
end
end
This way now you have the test scoped to the user.
I am assuming that you have device for user management
I have a form. Posting array variables from this form in the same name. (Sorry for my bad English.)
Here is the example:
<input type="text" name="name[]" class="form-control">
It is coming like ["a", "b", "c"]
I need save this variables to mysql from array. Not with array. Just one by one.
Create from my post controller
array_length = params[:type].size
for i in 0..array_lenth
type = params[:type][i]
name = params[:name][i]
title = params[:title][i]
value = params[:value][i]
#And save code.
end
I know i can save with some normal sql query but i dont know should i use or how i can use params require permit . something like
def form_params
params.require(:form).permit(:id, :type, :name, :value, :title, :post_id)
end
If i use this, how can i determine the necessary variables. Please help me and sorry for my English :)
Note: I have new post form and i have to add some variables to another table in post form (it is those arrays)
You have to specify that the param is an actual array, something like:
def form_params
params.require(:form).permit(:id, :type, :value, :title, :post_id, name: [:name_of_attribute, :another_name_for_attribute])
end
And then for your form:
<div class="field">
<%= form.text_field "name[name_for_attribute]" %>
</div>
Where the name_for_attribute is the name of the attribute you want it to be, so on the controller you will read the params like:
"form" => {"name"=>{"name_for_attribute"=>"The entered value"}
You can change the name_for_attribute for whatever you want and if for some reason someone tries to add another weird thing into the array by inspecting the HTML, you will filter all the attributes you want anyway.
Hope this helps! Let me know how it goes
This must be a common need but I can't seem to find a definitive answer on the most rubyesque way. I need to create a fairly complex algorithm to dynamically calculate course grades in a rails 4.1 app.
Specifically, I have a model, "course", and whenever an instance of it is displayed in the view, I want to dynamically calculate the current grade (a decimal value, calculated from many course.field values) and display it as a letter value using a switch/case. My assumption was that I could do this in the controller (but it almost seems like it's complex enough to warrant it's own -- module? In C++ I would create a class). At the same time, since it is created dynamically, it seemed like bad form to create a current_grade field for it in the model, so it's not one I can pass back and forth as one of the allowable params (that I know of-- can one pass a variable in the params that is not represented in the db?).
In my initial research I see suggestions of hidden_field_tags and helper_methods and all_helpers and modules and global modules and more. Under time pressure, I dread beginning down the wrong path. Which is the better approach? Or a good high level doc for reference?
As an example, here is one view in which I would like to calculate current grade, compare it to desired grade, and display accordingly.
# index.html.erb
<% #courses.each do |course| %>
<li>
<%= my_algorithm_to_calculate_curr_grade(many course.fields used to caluculate)
<= course.desired_grade ? "set li to <Color: red>" : "set li to <Color: green>" %>
<%= course.course_name %>
Current Calculation: <%= display_results_of_previous_calculation %>
(Goal: <%= course.desired_grade %>)
<%= link_to 'Show', course %>
<%= link_to 'Edit', edit_course_path(course) %>
<%= link_to 'Drop Course Without Penalty', course, method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %>
It's hard to tell from your question if course.fields are attributes of Course or different model(s). If all the fields are Course attributes, I would put it as an instance method on Course.
class Course < ActiveRecord::Base
def calculated_grade
# fun algorithm
end
end
If course.fields need to be loaded from the database, I'd probably go with a Plain Old Ruby Object (PORO), maybe call it CourseGradeCalculator (put it in app/models, why not? It's business logic)
class CourseGradeCalculator
attr_reader :course, :fields, :grade
def initialize(course, fields)
#course = course
#fields = fields
#grade = calculate_grade
end
private
def calculate_grade
# fun algorithm
end
end
# controller
#course = Course.preload(:fields).find(params[:id]
# view
CourseGradeCalculator.new(#course, #course.fields)
I'm trying to have a drop down list but when i try it it give me
undefined method `collect' for nil:NilClass
the controller:
def existing
#courses = Course.all
end
def duplicate
course = Course.find_by_id(permitd_up[:id])
new_course = course.dup
if new_course.save
redirect_to :action => 'show'
else
redirect_to :back
end
end
the view:
<h3>Choose a Course</h3>
<%= form_for :course , url: {:action => "duplicate" , method: "post"} do |f|%>
<%= f.select :id , #courses.collect{|c| [c.id , c.name]} %>
<br><br>
<%= f.submit%>
<%end%>
You will receive the following error
undefined method `collect' for nil:NilClass
on
<%= f.select :id , #courses.collect{|c| [c.id , c.name]} %>
Only when #courses instance variable was not set in the action that rendered this particular view.
I see that #courses variable is set in the existing method. If you are using existing as an action which renders this view then your view name must be existing.html.erb.
Or if you are rendering the view from a different action then in that case you should set #courses value in that particular action by either directly setting the value within action OR by calling existing method from there.
If you have your courses as a database table, you might want to try using rails' built in field helper collection_select. It will populate your select field with all of the data available in your model. If you want a drop-down like the one you are describing, I believe using collection select is the best way to handle it.
You can read up on it here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
Alternatively, if you have a ton of courses, maybe try looking into using a text field with autocomplete. Jquery UI has a plugin that makes this very easy. You can check out the railscasts for it here: http://railscasts.com/episodes/102-auto-complete-association-revised.
It requires a pro account but if you do a lot of rails developing it will be the best $9 you spend every month.
If you would like to continue to do it this way, make sure that you are defining
#courses = Courses(:all) in the correct controller action, otherwise you will have nothing to render.
I don't like the way rails does page titles by default (just uses the controller name), so I'm working on a new way of doing it like so:
application controller:
def page_title
"Default Title Here"
end
posts controller:
def page_title
"Awesome Posts"
end
application layout:
<title><%=controller.page_title%></title>
It works well because if I don't have a page_title method in whatever controller I'm currently using it falls back to the default in the application controller. But what if in my users controller I want it to return "Signup" for the "new" action, but fall back for any other action? Is there a way to do that?
Secondly, does anyone else have any other ways of doing page titles in rails?
I disagree with the other answers, I believe the title shouldn't be set per action, but rather within the view itself. Keep the view logic within the view and the controller logic within the controller.
Inside your application_helper.rb add:
def title(page_title)
content_for(:title) { page_title }
end
Then to insert it into your <title>:
<title><%= content_for?(:title) ? content_for(:title) : "Default Title" %></title>
So when you are in your views, you have access to all instance variables set from the controller and you can set it there. It keeps the clutter out of the controller as well.
<%- title "Reading #{#post.name}" %>
I like to put a catchall, default title in my layout that can be overridden from an action by setting #title:
<title><%= #title || "Default Title Here" %></title>
Then you can generate a title in your action:
def show
#post = Post.find_by_id params[:id]
#title = "tybro's blog: #{#post.title}"
end
I would do this:
# Application Controller
before_filter :set_page_title
private
def set_page_title
#page_title = "Default Title"
end
overwrite it in your other controllers
# Users Controller
before_filter :set_page_title
def new # in Users controller
...
#page_title = "Sign up"
...
end
private
def set_page_title
#page_title = "Users"
end
In your view:
<title><%= h #page_title %></title>
Have a look at Ryan Bates (ryanb from railscasts.com) nifty layout. He has a method in there that does what you are looking for. It's similar to Garrett's way, only he moves the default values in the helper too. Check out the helper.rb file and how he link textuses it.
You can install is as gem (and use the other nice features) or just use his way of doing it. You only need to pass the title value to title in each view (or let it fall to the default) and there you go. I'm with Garrett to put these things in the view.
Layout template
In your layout (e.g. application.html.erb) add:
<title><%= content_for(:page_title) || "Fallback title" %></title>
Page template
In the page template you'd like to a specify a title from:
<%- content_for(:page_title, "Specific page title") %>
class ApplicationController < ActionController::Base
before_action :set_page_title
private
def set_page_title
#page_title = t(".page_title", default: '').presence || t("#{controller_name}.page_title", default: '').presence || controller_name.titleize
end
end
I recently started taking this approach then outputting #page_title in the layout. It seems to work quite nicely for me.