I'm writing a rails app, and i have an Article Model. This model has two attributes of issue_id and article_position
In the show view i'm attempting to make a button that goes to the "next" article in a series. the articles go in order from 1 to whatever.
How can i properly do the following call (Psuedo-code):
<%= link_to 'Next', #article_url.where(#article.article_posistion = current_article.article_position + 1, #article.issue_id = current_article.issue_id) %>
I'm pretty stuck as of right now, if i need to be using find, let me know.
thanks!
You can define a method on the Article model that would do what you want:
def next_article
Article.where(issue_id: issue_id, article_position: article_position + 1)
end
Then in your controller you could do:
#next_article = #article.next_article
And in your view:
<%= link_to 'Next', article_path(#next_article) if #next_article %>
This should work how you want, assuming you have article_path defined as a named route.
Related
I have a model called sections, which has many feedbacks. This is working ok, and on the 'section' show page I can show the associated 'feedbacks' like so:
<% #section.feedbacks.each do |feedback| %>
<%= feedback.name %>
<%= link_to 'Show', feedback %>
<%= link_to 'Edit', edit_feedback_path(feedback) %> |
<% end %>
Works fine. But now I need a button that takes me to the create page for a new feedback item, and it needs to be associated to this 'section'.
At first I did it with a nested form, but the feedback items have quite a lot of fields and so it's messy to do it all on one page.
I'm new to ruby, so hopefully it's a really simple thing!
Thanks in advance for any help,
James
You should use nested form for it. If your form contains many fields then use bootstrap wizard for it.
or
<%= link_to 'New', new_feedback_path(section_id: #section.id) %>
& in your new method of feedback_controller , write the below:
#feedback = Feedback.new
#feedback.section_id = params[:section_id]
What you are trying to do is pretty much standard Rails stuff and you need to read a bit more of the official guides. You will need:
A controller with different actions for feedbacks (edit and new for example)
Some views for the above actions. The edit and new views can share the same form.
Routing to make it possible to work with a Feedback in the context of a Section.
Like:
resources :sections do
resources :feedbacks
end
This will allow you to use the following instead of your edit link:
<%= link_to 'Edit', edit_feedback_path([#section, feedback]) %>
And will go to this edit route:
/sections/:section_id/feedbacks/:feedback_id
You will also have the following new route available:
/sections/:section_id/feedbacks/new
Which will allow you to get the right section from the url params in order to create a feedback for it.
I'm pretty new to rails and I'd like to set my links for a certain page dynamically. I have a table called "Unfinished" and it has a column called "link" (corrected from "links") I'd like to be able to call the "link" record in the view to set my link_to link path.
I am trying to do this...
<%= link_to #unfinished.link(:p => #post.id) do %> FINISH <% end %>
...but that's not working.
my controller says:
def show
#post = Post.find(params[:id])
#unfinished = Unfinished.where('progress = ?', #post.progress).last
end
and the controller logic works fine...until I try to put the #unfinished.link into link_to
Edit:
Error Message:
wrong number of arguments (1 for 0)
Model
class Unfinished < ActiveRecord::Base
end
The type of links are :
step1_path
step2_path
step3_path
I am making a multipage form that you can save partway through. Based on a value in the #post.progress column (like 1, 2, 3) the correct path to complete the post will be provided (step1_path, step2_path etc...)
try this.
<%= link_to eval(#unfinished.link.to_s) do %> FINISH <% end %>
since the link you want is actually a named route, so you will need to eval it.
but with this you wouldn't be able to be able to pass in the post id, which you will need.
If the route is the same for all records (save for what part you are on based on the progress attribute) do you even need to store it in the database? You could just make the link method return the path (that you would still need to eval).
something like
def link (post)
"step#{self.progress}_path(post.id)"
end
and then eval the link on the way back. but Not sure if that will work, just thinking out loud...
There are gems that do multi-stage forms perhaps looking into them might help?
I've been stuck on this problem for days. First off, I now know this code is horribly wrong. I've been trying to fix it, but it's way more important in the short term that this link is created. In my view (I'm so sorry), I call the create method like this, if a certain condition is met:
index.html.erb (controller: subjects_controller)
<%= Baseline.create(subject_id: sub.subject_id) %>
I do this several times on the page, from several controllers (i.e., FollowUp3Week.create(subject_id: sub.subject_id) works). All of the other controllers work. I've checked, and double checked, every controller action and compared them to each other, and they appear the same.
So instead of creating the record, it leaves something like this instead:
#<Baseline:0x007f944c4f7f80>
I'm at a bit of a trouble shooting loss. Once again, I know how wrong it is to have these database actions in the view. But I didn't know that when I made the page, and I really need this to function before I can take the time to learn how to rearrange everything through the MVC.
Any advice would be greatly appreciated. Let me know what other code you might want to look at.
EDIT 1.
link Creation:
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path(Baseline.where(subject_id: sub.subject_id).first) %>
<% else %>
<%= Baseline.create(subject_id: sub.subject_id) %>
<% end %>
First of all, making DB calls in views is a big NO! NO!
Secondly, to answer why you see the output as
#<Baseline:0x007f944c4f7f80>
for
<%= Baseline.create(subject_id: sub.subject_id) %>
You are trying to render an instance of Baseline model. Its just how the instance would be displayed. If you want to display a particular attribute's value in view then just do
<%= Baseline.create(subject_id: sub.subject_id).subject_id %>
Also, this code will not create a link. To create a link you would have to call link_to helper in your view.
What you need to do is, move the Baseline.create call in the controller. Set an instance variable in the action which renders this particular view as below:
def action_name
#baseline = Baseline.create(subject_id: sub.subject_id)
end
After this in you view you can easily access all the attributes of #baseline instance.
For example:
To access subject_id
<%= #baseline.subject_id %>
To create a link for show page of #baseline, provided you have a RESTful route to show action for baselines
<%= link_to "Some Link", #baseline %>
I am calling a random Post and allowing users to +1 or -1 the post before loading another post. My model generates a random record at the URL /posts/random using the following.
Post.rb // Model
def self.find(*args)
if args.first.to_s == "random"
Post.find :first, :offset => rand(Post.count)
else
super
end
This code generates a random post when the user visits posts/random. However, I also defined a thumbs_up and thumbs_down field in the database that correspond to a thumb_up and a thumb_down image that when clicked, need to +1 or -1 the value in the database, before redirecting to another /posts/random. My understanding is that to do this I need to invoke the PUT update method, however, since my URL is posts/random instead of posts/1, how can I do this? Do I need to alter my routes?
First thing is that you'd need is an instance variable representing the random post so that you could use it in the thumbs up and thumbs down links. Then you need to make sure that those links look something like this:
<%= link_to "Thumbs Up", post_path(#post, thumbs_up: 1), method: :put %>
<%= link_to "Thumbs Down", post_path(#post, thumbs_down: 1), method: :put %>
That way, when in your update action for your posts controller, you can do something like this:
#post.update_attributes(thumbs_up: #post.thumbs_up + params[:thumbs_up],thumbs_down: #post.thumbs_down + params[:thumbs_down] )
That is only a start, but I think that should get you going in the right direction.
I am working a simple rails app and i would like to know how possible it is to use one search form to search inside multiple models. like i have a story model and a book model. this search form should be able to search the both models with a single parameter.
<%= for_tag :url => search_path %>
<%= text_field_tag :q %>
<% end %>
How can i make this search from work for multipple models
Whatever search you need to do, is done inside an action in a controller. You could basically create a controller, say search_controller and have an action say, item
def item
if params[:q]
#found_stories = Story.find_all_by_...(params[:q])
#found_books= Book.find_all_by_...(params[:q])
end
end
Then you could use the objects #found_stories and #found_books in your view to show the search results.
This is just an example of how you could do to fulfill your requirement.
Thanks.