def self.find_by_rank(id, rank) @this_article = find(id)
if (rank == "up") @next_article = find_by(ranking: @this_article.ranking.to_i-1) else @next_article = find_by(ranking: @this_article.ranking.to_i+1) endend
def rankup self.ranking = self.ranking - 1end
def rankdown self.ranking = self.ranking + 1end
And in my controller, I have this:
def rank @this_article = Article.find(params[:id]) @affected_article = Article.find_by_rank(params[:id], params[:rank])
if (params[:rank] == 'up') @this_article.rankup @affected_article.rankdown else @this_article.rankdown @affected_article.rankup end
@this_article.save @affected_article.save
redirect_to articles_path()end
This is the best way I can think of the move the self.save to the controller while keeping the majority of the logic in the model.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/47a54481-78ec-4467-923d-bef5e5c6fb31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks a lot! :-)
2015년 7월 1일 수요일 오전 4시 53분 59초 UTC+9, Frederick Cheung 님의 말:
On Tuesday, June 30, 2015 at 8:43:48 PM UTC+1, Yoon-Ho Choi wrote:--
> Hi~
>
>
> I have raspberry pi 2 and odroid c1 server. (arm v7)
> There's same trouble on rails 4.2.x(4.2.0~4.2.3)
> I can not connect rails server page. (there's no error)
> I could not find a log(blank file).
> I'm using rbenv. Already, I had tested ruby source compile, too.
>
>
> But There was not problem on rails 4.1.x
One of the changes in 4.2 was to change rails server to bind to localhost only by default - you can only connect to the server from the same machine. To restore the old behaviour you can pass the -b option eg
rails s -b 0.0.0.0 #listens on all interfaces
Or
rails s -b 192.168.1.124 #listen on that particular interface
Fred.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2c758959-02b0-4d03-996d-42bdc3cde29c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 30, 2015 at 8:43:48 PM UTC+1, Yoon-Ho Choi wrote:
> Hi~
>
>
> I have raspberry pi 2 and odroid c1 server. (arm v7)
> There's same trouble on rails 4.2.x(4.2.0~4.2.3)
> I can not connect rails server page. (there's no error)
> I could not find a log(blank file).
> I'm using rbenv. Already, I had tested ruby source compile, too.
>
>
> But There was not problem on rails 4.1.x
One of the changes in 4.2 was to change rails server to bind to localhost only by default - you can only connect to the server from the same machine. To restore the old behaviour you can pass the -b option eg
rails s -b 0.0.0.0 #listens on all interfaces
Or
rails s -b 192.168.1.124 #listen on that particular interface
Fred.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a3888a9b-72a5-40f6-a7ac-c138702510de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 30, 2015 at 6:58:38 PM UTC+1, Jason Hsu, Ruby on High Speed Rails wrote:
> The code in question works, but Rails Best Practices docks me with a scope access warning. The code in question is:
>
> def show
> # NOTE: rails_best practices recommends using scope access
> redirect_to(root_path) unless current_user == User.find(params[:id])
> @user = User.find(params[:id])
> end
>
> How can I get this code to comply with the scope access standard?
I think you've slightly confused it. It's trying to warn you against doing
post = Post.find params[:id]
if post.user == current_user
...
end
Because it's better to do
current_user.posts.find(params[:id])
Which doesn't apply in your case because it's users you are fetching, not some collection that belongs to a user. I'm not sure why you're bothering with the id parameter at all - why not
@user = current_user
?
Fred
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/59924f91-bdea-4112-a21b-aadd87724f3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7e45eb0-ae1b-416f-880f-271ceb1db6b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7381b8a-ad97-478f-80e0-ffd9784e7f52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 30, 2015 at 1:58:38 PM UTC-4, Jason Hsu, Ruby on High Speed Rails wrote:
The code in question works, but Rails Best Practices docks me with a scope access warning. The code in question is:
def show
# NOTE: rails_best practices recommends using scope access
redirect_to(root_path) unless current_user == User.find(params[:id])
@user = User.find(params[:id])
end
How can I get this code to comply with the scope access standard?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1416593c-4e23-4af2-a043-b5f966f95971%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
def show
# NOTE: rails_best practices recommends using scope access
redirect_to(root_path) unless current_user == User.find(params[:id])
@user = User.find(params[:id])
end
How can I get this code to comply with the scope access standard?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/98301ec8-a9b0-4bda-982a-34417a67e173%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
There's same trouble on rails 4.2.x(4.2.0~4.2.3)
I can not connect rails server page. (there's no error)
I could not find a log(blank file).
I'm using rbenv. Already, I had tested ruby source compile, too.
=> Booting WEBrick
=> Rails 4.2.3 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-07-01 02:24:55] INFO WEBrick 1.3.1
[2015-07-01 02:24:55] INFO ruby 2.0.0 (2013-11-22) [armv7l-linux-eabihf]
[2015-07-01 02:24:55] INFO WEBrick::HTTPServer#start: pid=21393 port=3000
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/27721c97-4ce3-491f-a9a6-85d8cc4b0cb1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 30, 2015 at 2:07:40 AM UTC-5, Ruby-Forum.com User wrote:
Maybe take out the save from rankup and rankdown and save in the
controller?
--
Posted via http://www.ruby-forum.com/.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9bdd8729-4f78-4a40-a704-5b1d8b47f98e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Monday, June 29, 2015 at 7:40:58 PM UTC+1, Мурыгин Антон wrote:
My rails version is 4.2.2.
понедельник, 29 июня 2015 г., 20:00:27 UTC+3 пользователь Frederick Cheung написал:
On Monday, June 29, 2015, Мурыгин Антон <mib...@gmail.com> wrote:Now this is my model
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing? Am i missing something?
super: no superclass method `category' for #<Business:0x000001023014b8>
Which version of rails?Fred--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com .
To post to this group, send email to rubyonrails-talk@googlegroups.com .
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .fabf3f0c-db07-44b4-a04c- 0b060eb9eaa5%40googlegroups. com
For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1964f21d-09bc-498f-8704-5504abae4054%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Tuesday, June 30, 2015 at 10:07:43 AM UTC+1, Rails Goa wrote:
13:56:19 web.1 | E, [2015-06-30T13:56:19.368604 #12225] ERROR -- : reaped #<Process::Status: pid=12247,exited(1)> worker=0See the ==> <NoMethodError: undefined method `application' for Rails:Module>Any suggestions. I'm at my wits end. tried everything...... And I just can't see anything in port 5000!
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e0841338-4683-4685-817e-5603a7a82251%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
So he's asked me to help him take it off heroku (ok done that got all the code and db and everything).... and he wants to move it to a new / cloud server.
So ok. I've set-up an Ubuntu 14.x LTS Server for him and got the code oj to that. That's done and fine.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f4b1b2c3-f181-4264-a187-500f928882a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Maybe take out the save from rankup and rankdown and save in the
controller?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ae2a6b5a451f06382ee28b68ed5b47e0%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6be0b0c1-8851-4115-8bce-04f98119ccc8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On 29 June 2015 at 21:27, Federicko <fedomain@gmail.com> wrote:
>> Hi All,
>
>
> I have moved everything over to the model as suggested. Check it out :p
>
> In the model, I created a Class method as well as the previous instance
> methods:
>
> class Article < ActiveRecord::Base
>
> def self.rank(id, rank)
> @this_article = find(id)
>
> if (rank == "up")
> @next_article = find_by(rank: @this_article.rank.to_i-1)
>
> @this_article.rankup
> @next_article.rankdown
> else
> @next_article = find_by(rank: @this_article.rank.to_i+1)
>
> @this_article.rankdown
> @next_article.rankup
> end
> end
>
> def rankup
> self.rank = self.rank - 1
> self.save
> end
>
> def rankdown
> self.rank = self.rank + 1
> self.save
> end
>
> end
>
> And now in my controller, I only have one action:
>
> def rank
> Article.rank(params[:id], params[:rank])
> redirect_to articles_path
> end
Rather than having a class method rank() I think it would be better to
have a member method then in the controller do something like
this_article = Article.find(params[:id])
this_article.rank( params[:rank] )
Also don't forget to allow for the fact that there might not be an
article with the given id, so perhaps
this_article.rank (params[:rank]) if this_article
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLssAGSx0Mq_S%2BPFeBOHZRJsNVBtsQweBcHcfB%2B0k6vxGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Hi All,
class Article < ActiveRecord::Base
def self.rank(id, rank) @this_article = find(id)
if (rank == "up") @next_article = find_by(rank: @this_article.rank.to_i-1)
@this_article.rankup @next_article.rankdown else @next_article = find_by(rank: @this_article.rank.to_i+1)
@this_article.rankdown @next_article.rankup endend
def rankup self.rank = self.rank - 1 self.saveend
def rankdown self.rank = self.rank + 1 self.saveend
end
And now in my controller, I only have one action:
def rank Article.rank(params[:id], params[:rank]) redirect_to articles_pathend
However, can anyone please review the code and let me know if it is correct in terms of OOP?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d9630b45-6824-4ab6-9f11-62b677df039f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On 29 June 2015 at 18:54, William de Lima <perfectbluex@gmail.com> wrote:
> ..
> ActionView::Template::Error (undefined method `name' for nil:NilClass):
> 9: .pull-right
> 10: = submit_tag t('news.comment'), class: 'btn btn-primary'
> 11: .clearfix
> 12: - unless @item.comments.any?
> 13: #comment-composer= t('news.no_comments')
> 14: - else
> 15: - @comments.each do |comment|
> app/views/news/show.html.haml:12:in
> `_app_views_news_show_html_haml___3582658555150926440_64556152601540'
Is @item nil possibly?
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsLzc4b8T%3DTsL%2B8Ynj0v%3DhXu%3Di%2BdGnsrNLEnTCdPPJ1zg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
On 29 June 2015 at 19:42, Federicko <fedomain@gmail.com> wrote:
>> Hi Fred,
>
>
> I took your advice and tried to move the ranking logic over to the model.
> I've also consolidated the 2 actions.
>
> Here's what I got:
>
> In the controller:
>
> def rank
> @this_article = Article.find(params[:id])
>
> if (params[:rank] == 'up')
> @next_article = Article.find_by(rank: @this_article.rank.to_i-1)
>
> @this_article.rankup
> @next_article.rankdown
> else
> @next_article = Article.find_by(rank: @this_article.rank.to_i+1)
>
> @this_article.rankdown
> @next_article.rankup
> end
>
> @this_article.save
> @next_article.save
>
> redirect_to articles_path
> end
>
> In the view, I am passing in the query string rank to indicate whether we
> are ranking up or down.
>
> And in my model I added two new methods:
>
> def rankup
> self.rank = self.rank - 1
> end
>
> def rankdown
> self.rank = self.rank + 1
> end
>
> I've added the rank up / rank down as a method in the Article object.
> However, the key to my ranking logic is to first find the next record as I
> need to swap the ranking with the current record. So the code to finding the
> next record (either previous or next record depending on whether we are
> ranking up or down). This code can only reside in the controller as far as I
> can tell.
Why do you think you cannot move more to the model? Which bit of code
in particular cannot reside there?
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt93uh4%2B8-zMzY1WkFNGBGivJFCeO5SM9tKYvrFwbJUiA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Hi Fred,
def rank @this_article = Article.find(params[:id])
if (params[:rank] == 'up') @next_article = Article.find_by(rank: @this_article.rank.to_i-1)
@this_article.rankup @next_article.rankdownelse @next_article = Article.find_by(rank: @this_article.rank.to_i+1)
@this_article.rankdown
@next_article.rankup
end
@this_article.save @next_article.save
redirect_to articles_pathend
In the view, I am passing in the query string rank to indicate whether we are ranking up or down.
def rankup self.rank = self.rank - 1end
def rankdown self.rank = self.rank + 1end
I've added the rank up / rank down as a method in the Article object. However, the key to my ranking logic is to first find the next record as I need to swap the ranking with the current record. So the code to finding the next record (either previous or next record depending on whether we are ranking up or down). This code can only reside in the controller as far as I can tell.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/95a528c4-37ae-4a23-83b3-329aab76d471%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
понедельник, 29 июня 2015 г., 20:00:27 UTC+3 пользователь Frederick Cheung написал:
On Monday, June 29, 2015, Мурыгин Антон <mib...@gmail.com> wrote:Now this is my model
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing? Am i missing something?
super: no superclass method `category' for #<Business:0x000001023014b8>
Which version of rails?Fred--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com .
To post to this group, send email to rubyonrails-talk@googlegroups.com .
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .fabf3f0c-db07-44b4-a04c- 0b060eb9eaa5%40googlegroups. com
For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4335c57a-d26d-48f8-84ca-c98ca897b4be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/75143d8d-3c59-4fda-9cab-05e84b90fed4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Monday, June 29, 2015, Мурыгин Антон <mibus32@gmail.com> wrote:
Now this is my model
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing? Am i missing something?
super: no superclass method `category' for #<Business:0x000001023014b8>
----
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fabf3f0c-db07-44b4-a04c-0b060eb9eaa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CABp077WDW1ASHDV9rszhna6BcBpke-vEfxG%2BRqx-%3DFH6uUzOCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
On Sunday, June 28, 2015 at 1:51:57 PM UTC-5, Federicko wrote:
Hi All,I am learning rails at the moment and have gone through one of the tutorials on the rails website for creating a simple blog system.I have added some new features to it and it is working great.However, I would like to show someone my code and see if it is the right or most efficient way of achieve this.This system is based on the blog system from the Getting Started with Rails guide which can be found on http://guides.rubyonrails.org/getting_started.html I simply added a rank up/rank down function to the blog system:First, in my routes.rb I added:
resources :articles doresources :commentsmember doget 'rankup'get 'rankdown'endend
Then, in my controller I added two new actions:
def rankup@this_article = Article.find(params[:id])@new_rank = @this_article.rank.to_i-1@prev_article = Article.find_by(rank: @new_rank)@prev_article.rank = @this_article.rank@this_article.rank = @new_rank@this_article.save@prev_article.saveredirect_to articles_pathenddef rankdown@this_article = Article.find(params[:id])
@new_rank = @this_article.rank.to_i+1@next_article = Article.find_by(rank: @new_rank)@next_article.rank = @this_article.rank@this_article.rank = @new_rank
@this_article.save@next_article.saveredirect_to articles_pathend
I also updated the destroy action to include a re ranking function:
def destroy@article = Article.find(params[:id])@start_rank = @article.rank@next_articles = Article.where(["rank > ?", @start_rank]).order('rank ASC')
@next_articles.each do |article|article.rank = @start_rankarticle.save@start_rank = @start_rank + 1
end@article.destroyredirect_to articles_pathend
And in the view I simply added the links to the list:
<% @articles.each.with_index do |article, index| %><tr><td><%= article.title %></td><td><%= article.text %></td><td><%= article.rank %></td><td><%= link_to 'View', article_path(article) %></td><td><%= link_to 'Edit', edit_article_path(article) %></td><td><%= link_to 'Delete', article_path(article), method: :delete, data: {confirm: 'Are you sure?'} %></td><td><% if index != 0 %><%= link_to 'Up', rankup_article_path(article) %><% end %></td><td><% if index != @articles.count-1 %><%= link_to 'Down', rankdown_article_path(article) %><% end %></td></tr><% end %>
As mentioned, I am new to RoR so I don't know if I'm doing this correctly according the Rails convention but the code is working great so I'm happy about that.If someone can review my code please and tell me what I can improve on, that would be great.I'm also thinking there might be an existing gem or something that I can install that will do the ranking for me automatically.Anyway, look forward to your feedbacks.Thanks in advance.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21414ebc-17c6-4a66-8231-0db8efde7ecb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
понедельник, 29 июня 2015 г., 17:05:42 UTC+3 пользователь Dave Aronson написал:
On Monday, June 29, 2015, Мурыгин Антон <mib...@gmail.com> wrote:
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing?
super: no superclass method `category' for #<Business:0x000001023014b8>
Possibly because AR::Base has no idea about your renaming of that association. Try calling that method business_category instead, or call business_category instead of super. (Just don't do both,)
--
Sent from Gmail Mobile; please excuse top posting, typos, etc. :-(
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/58367a31-7dba-4d0a-9bbd-3a4ada3d14bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On Jun 29, 2015, at 8:18 AM, Padmahas Bn <padmahas@gmail.com> wrote:
> Hello Elizabeth McGurty that worked!!! (with little modification)
>
> Unique id for each modal generated fine but when calling that modal the the link_to didn't worked for me.
>
> I had to change this
> <td><%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %></td>
>
> To this:
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
>
> <td><%= link_to 'Show', designation_dict, "data-target" => "#myModal_#{designation_dict.id}", "data-toggle" => "modal", :class => 'btn btn-default' %></td>
A quick tip for generating HTML ID attributes from Rails: there is a helper method named #dom_id. You use it like this: dom_id(designation_dict), and by default, you get [object classname]_[numerical id] as a string. It takes an optional prefix as the second argument, which gets prepended, so if you did dom_id(designation_dict, :edit), you would get edit_designation_dict_42 out of it. This comes in really handy when you are setting up an ID in a view, and then again in a UJS file or similar. You don't have to fiddle around with a lot string concatenation in two places that way.
Walter
>
> Working like a charm. Thank you a lot. I was searching for this from past 20 days.
>
> Thank you again.
>
> On Mon, Jun 29, 2015 at 12:27 AM, Elizabeth McGurty <emcgurty2@gmail.com> wrote:
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
>
> In your locals you are sending designation_dict. Is there something in designation_dict that is unique to all the records? Hopefully an id...
>
> Do you have that unique field, let's call it dd_id?
>
> Now this is going to seem to be Ad nauseam, and Mr Law refers to id="MyModal", which I do not see, yet he is entirely correct. What I understand is that you have an each statement that is iteratively loading...A partial, and numerous link_to
>
> For later, however you need it as params or js elements, each of these elements must contain -- at some required element level -- a unique html id.
>
> Look at the changes I made to your partial:
>
> <div class="modal-body" id="modal-body_<%= designation_dict.dd_id %>">
> <div class="table-responsive" id="table-responsive_<%= designation_dict.dd_id %>">
> <table class="table table-striped table-show">
> <tr>
> <th>
> <strong>Designation code:</strong>
> </th>
> <td>
> <%= designation_dict.desig_code %>
> </td>
> </tr>
>
> <tr>
> <th>
> <strong>Designation description:</strong>
> </th>
> <td>
> <%= designation_dict.designation %>
> </td>
> </tr>
> </table>
> </div>
>
> Do you see now how you are generating unique html ids?
>
> <td><%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %></td>
>
>
> Once you make these changes, and then View Page Source, you will see that a unique id has been generated.... Folks will probably offer more elegance solutions, but the spirit remains the same.
>
> Hope this helps...
>
> Liz
>
>
> On Monday, May 4, 2015 at 10:47:06 AM UTC-4, Padmahas Bn wrote:
> After following so many Stackoverflow and other related posts I was able to render modal (_show.html.erb) from inside index.html.erb. But the problem the parameter I'm sending is showing same id and other details for all show buttons.
>
> For instance if there are 5 different members listed in index.html.erb, when I press the corresponding show button, the same id and other details as the first member is showing for all members.
>
> index.html.erb
> <tbody>
> <% @designation_dicts.each do |designation_dict| %>
> <tr>
> <td><%= designation_dict.desig_code %></td>
> <td><%= designation_dict.designation %></td>
>
> <td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
> <td><%= link_to 'Show', "#myModal", "data-toggle" => "modal", :class => 'btn btn-default' %></td>
> <td><%= link_to 'Edit', edit_designation_dict_path(designation_dict), :class => 'btn btn-default' %> </td>
> <td><%= link_to 'Destroy', designation_dict, method: :delete, data: { confirm: 'Are you sure?' } , :style => 'color:#FFFFFF', :class => 'btn btn-danger' %></td>
> </tr>
> <% end %>
> </tbody>
>
> Part of the modal (_show.html.erb)
> <div class="modal-body"> <div class="table-responsive">
> <table class="table table-striped table-show">
> <tr>
> <th>
> <strong>Designation code:</strong>
> </th>
> <td>
> <%= designation_dict.desig_code %>
> </td>
> </tr>
>
> <tr>
> <th>
> <strong>Designation description:</strong>
> </th>
> <td>
> <%= designation_dict.designation %>
> </td>
> </tr>
> </table>
> </div>
>
> Thank you
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/17d2a1b5-f7ed-40b7-acf0-daf0a70340a0%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAC_h78%2BSVSs_8uU%3DGAHYOHtiKijn7U4oiy%3DjV55HDyP-x5dADw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21DD5BFE-B6C0-493B-B747-4DD20089C67D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
On Monday, June 29, 2015, Мурыгин Антон <mibus32@gmail.com> wrote:
class Business < ActiveRecord::Base
belongs_to :category, class_name: 'BusinessCategory', foreign_key: 'business_category_id'def categorysuper || BusinessCategory.new(name: 'other')endend
Why it keeps throwing?
super: no superclass method `category' for #<Business:0x000001023014b8>
--
Sent from Gmail Mobile; please excuse top posting, typos, etc. :-(
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQijrynpvtDbig_5GNwAHq1W0mQqBkp5nRFZNyBTMON_bNw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
<td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td>
In your locals you are sending designation_dict. Is there something in designation_dict that is unique to all the records? Hopefully an id...
Do you have that unique field, let's call it dd_id?
Now this is going to seem to be Ad nauseam, and Mr Law refers to id="MyModal", which I do not see, yet he is entirely correct. What I understand is that you have an each statement that is iteratively loading...A partial, and numerous link_to
For later, however you need it as params or js elements, each of these elements must contain -- at some required element level -- a unique html id.
Look at the changes I made to your partial:
<div class="modal-body" id="modal-body_<%= designation_dict.dd_id %>">
<div class="table-responsive" id="table-responsive_<%= designation_dict.dd_id %>">
<table class="table table-striped table-show">
<tr>
<th>
<strong>Designation code:</strong>
</th>
<td>
<%= designation_dict.desig_code %>
</td>
</tr>
<tr>
<th>
<strong>Designation description:</strong>
</th>
<td>
<%= designation_dict.designation %>
</td>
</tr>
</table>
</div>
Do you see now how you are generating unique html ids?
<td><%= link_to 'Show', "data-toggle" => "modal", :class => 'btn btn-default', :id => designation_dict.dd_id %></td>
Once you make these changes, and then View Page Source, you will see that a unique id has been generated.... Folks will probably offer more elegance solutions, but the spirit remains the same.
Hope this helps...
Liz
On Monday, May 4, 2015 at 10:47:06 AM UTC-4, Padmahas Bn wrote:After following so many Stackoverflow and other related posts I was able to render modal (_show.html.erb) from inside index.html.erb. But the problem the parameter I'm sending is showing same id and other details for all show buttons.For instance if there are 5 different members listed in index.html.erb, when I press the corresponding show button, the same id and other details as the first member is showing for all members.index.html.erb<tbody><% @designation_dicts.each do |designation_dict| %><tr><td><%= designation_dict.desig_code %></td><td><%= designation_dict.designation %></td><td><%= render :partial => "show", :locals => {:designation_dict => designation_dict} %></td><td><%= link_to 'Show', "#myModal", "data-toggle" => "modal", :class => 'btn btn-default' %></td><td><%= link_to 'Edit', edit_designation_dict_path(designation_dict), :class => 'btn btn-default' %> </td><td><%= link_to 'Destroy', designation_dict, method: :delete, data: { confirm: 'Are you sure?' } , :style => 'color:#FFFFFF', :class => 'btn btn-danger' %></td></tr><% end %></tbody>Part of the modal (_show.html.erb)<div class="modal-body"> <div class="table-responsive"><table class="table table-striped table-show"><tr><th><strong>Designation code:</strong></th><td><%= designation_dict.desig_code %></td></tr><tr><th><strong>Designation description:</strong></th><td><%= designation_dict.designation %></td></tr></table></div>Thank youTo view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/17d2a1b5-f7ed-40b7-acf0-daf0a70340a0%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAC_h78%2BSVSs_8uU%3DGAHYOHtiKijn7U4oiy%3DjV55HDyP-x5dADw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.