was wondering if anyone can help me.
I'm just starting ou so this may seem a bit mundane to some people. I
have a view which I need to display certain fields if a particular
value is selected in the database.
The example is similar to a blog post where you may or may not want to
show a source link. So in the backend there is a tick box which is
attached to a sql boolean value then if that is ticked you can also
put the link in for the source location.
On the front end however if the boolean is set to false I don't want
to display the data in that field. What's the best way of going about
this?
You can use conditionals in erb views.
For example:
<% if #v == 4 %>
<p>v is equal 4.</p>
<% else %>
<p>v isn't equal 4.</p>
<% end %>
Related
First of all, I've done a fair amount of looking around, and while questions get around answers, I have a problem I think is somewhat unique. I have a list of checkboxes generated with the following code:
<% for student in Student.find(:all) %>
<div>
<%= check_box_tag "user[student_ids][]", student.id, current_user.students.include (student) %>
<%= student.name %>
</div>
<% end %>
After clicking the 'update' button at the bottom, I need each of the checked boxes to be placed into an array. I then plan on iterating over the array and doing some work on each of the checked names. I am having a hard time, however, with the process of getting these names all into an array. I really am not sure which of the standard web actions this kind of work should be (i.e, post, get, etc.), so I don't know how to set up a route. Even if I could set up a route to a controller, how would I get the checked students into an array of Student objects?
Thanks ahead of time for your help!
The full answer to your question depends on a variety of things, for example, what you are trying to do with the submitted array, etc (which would determine whether POST, GET, PUT or DELETE should be used.) Without knowing more information with respect to your code base, if you throw the following code into a form_for in one of your controller's already restful routes, you should be able to see the array of checked names:
<%= current_user.students.include(student).each do |student| %>
<div>
<%= check_box_tag "student_names[]", student.name %> <%= label_tag student.name %>
</div>
<% end %>
Then, when the user hits submit, the params hash will show student_names = [].
And make sure your attributes are accessible as needed.
On a side note, check out Railscasts pro episode from last week. Pretty much exactly explains what you are trying to do. It's a subscription service, though.
I managed to solve my problem in a less-than-satisfying way. Here is the code I ended up using:
current_user.students.delete_all
if(params.has_key? :user)
params[:user][:student_ids].each do |i|
current_user.students<<(Student.find(i))
end
end
Because the number of students I'm managing is not ever larger than 100, this operation isn't as bad as it looks. I'm deleting all of the associations already present, and then cycling through all passed parameters. I then find the student object with the passed parameter id and add it to the current_user's User-Student join table.
I hope this helps someone down the line!
So I have one model called Project, for which there is a nested model called Proposal (so every project has multiple proposals, and each proposal only belongs to one Project).
I have a column for Proposal called "winning" which just checks if one of the Proposals has won for the Project. I'd like to reference this on the Show page of the Project, but a little perplexed by the code.
What I really want to do is check if any of the proposals have status "winning"
This is what I'm trying for the Show view for Projects, but it isn't working:
<% if #project.proposals.winning %>
SUCCESSFUL
<% end %>
I feel like this should be pretty rudimentary but I'm having trouble figuring it out, thanks!
That's ideal candidate for:
<% if #idea.proposals.any? {|proposal| proposal.winning? } %>
Enumerable.any? returns true if for any array element the block returns true.
Use it instead:
<% if #idea.proposals.count{|a| a.winning } > 0 %>
Or even better to create a method for it in the Idea model:
def has_winning?
proposals.count{|a| a.winning } > 0
end
Okay, found this code on another post and it seems to be working, not sure if it's the best way to go about it though:
<% if #idea.proposals.map(&:winning).flatten %>
I am trying to pass an extra variable, that determines whether or not the user has clicked on a particular checkbox, and this variable is not a part of my model. I want to make it so on the controller update function, it can have access to this variable, and see what it was set to. I have seen some other stack overflow answers for this type of problem, and it is generally suggested to do something using hidden_field_tag, something like this:
<% hidden_field_tag "blah", params[:test] %>
or
<% hidden_field_tag :example, "test" %>
When trying this, I did a params.inspect and could not find the "test" param variable, using both of the above options. Should I be trying to retrieve this hidden field tag in a different way? Will it be available in the update request to the controller? If not, does anyone know some way this is possible?
Open to any suggestions,
--Anthony
You would either do it like this
<%= hidden_field_tag 'test' , 'blah' %>
or you could do this
<%= hidden_field_tag :whatever_you_want , 'blah', {:name=>'test'}
You had the name and value reversed in your post. The really important thing is to make sure the form element has the name you want to show up in the params hash. The second example would generate the element with id='whatever_you_want' name='test'.
I have a dropdownmenu which populates from the database. I'm using the following code to do it:-
<%= collection_select(:abc, :SkillSetName, #technologies, :id, :Topic) %>
I have no idea what :abc and :SkillSetName are doing here. I just know that the drop down is being populated with :Topic from my #technologies variable. I want to save the selection made by the user from the drop down menu and send it to the next controller action. I don't want to use f.collection_select
If you don't know what :abc and :SkillSetName are, how are we supposed to know?
Anyway, the user's selection will be in:
params[:abc][:SkillSetName]
If you want to understand what you are doing, have a look at the API for collection_select.
See my answer - RoR: collection_select not setting the value in the DB
:abc stand for your object and :SkillSetName stand for your method.
when you want to save its value you can get it using params[:abc][:SkillSetName]
In Ruby on Rails 3 How would I create a view that decides by a parameter what link in view links to?
For example to a page in my view I pass a type parameter which displays all projects in my data base and depending on the type links to either the new show or edit action.
I am interested in only passing on the path of the link.
I would like to write something like:
<% link_to(enter_here_path) do %>
<div class="blah"><%=#project%></div>
<%end%>
You could use a conditional which returns the proper location or even creates the link, probably best wrapped in a helper method.
Something like that:
def your_link_method(type="delete")
case type
when "delete"
link_to …
when "foobar"
link_to …
else
link_to …
end
end
end
As a sidenote: This kind of construct smells IMO and I'd probably rethink my design first, before I implement a solution like this. Even if you can probably find a simpler and more elegant way to write it.