Ruby on Rails Tuesday, June 30, 2015

Hi All,

First of all, let me quickly clarify the meaning of ranking here.
All I'm doing is move an article up or down in the display on the index file.

Quick example. Below is a sample of the Articles table:

ID       title                                    ranking
5         Rails is cool                        1
6         Programming is fun            2
7         How to install Rails             3
8         Macbook Pro features        4

If a user click on article ID 7 to move it up, this is what the system should do:

Change the ranking for article ID 7 to 2
Change the ranking for article ID 6 to 3

Basically, swap them around.

So as you can see, when a user click on an article, the system needs to modify 2 records in the db.

Now, if I want to move the self.save to the controller, the best way I can think of the implement this is as follows:

IMPORTANT: I have renamed the column rank to ranking below

In my model, I have 3 methods:

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)
  end
end

def rankup
  self.ranking = self.ranking - 1
end

def rankdown
  self.ranking = self.ranking + 1
end

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.

Please let me know if there is a better way.

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/47a54481-78ec-4467-923d-bef5e5c6fb31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

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.

Ruby on Rails

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.

Ruby on Rails

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.

Ruby on Rails

Hello...
I would like to use Formtastic and set Formtastic configs inside a Rails Engine, without having those changes to form helpers/config 'leak' into my main application. 

Specifically I am trying to isolate an implementation of Formtastic that is customized for Bootstrap 3 to within my engine, without having the main app's form helpers start using the same markup.

I could specify the ":builder"  option in all my main app's calls to form helpers to override this 'leak', however it would probably be better for the engine's helpers not to leak into the main app in the first place?

Can anyone please offer some insight on how to go about that?

Any help appreciated,
-S

--
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.

Ruby on Rails

Colin is right about switching to Mac or Linux. If you really want to use Ruby & Rails, setup up a VM in VirtualBox. I use Linux Mint which is great for traditional Windows users. Be fearless and jump right in. Google Search is your friend.

--
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.

Ruby on Rails

Manage privilege at the model level...

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.

Ruby on Rails

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/98301ec8-a9b0-4bda-982a-34417a67e173%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

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

=====================================================
rails new test2
cd test2
bundle install
rails server
=====================================================
c1omv:~/work/test2# rails s
=> 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.

Ruby on Rails

Yes but I also call those method from the Class method rank and it needs to save.

This is always my dilemma, just exactly what or how much code should go into controller and model?
Like in my example, before when I had everything in the controller, people suggested that I put that code in the model.
So I did. But now, people are suggesting I take some code back out to the controller.

So exactly which part should go into the controller and which part belongs to the model?

Having said that, I do agree the self.save should be in the controller but this will break my self.rank Class method. Arrrgh!

Any suggestion how I can break down the self.rank Class method so I can put the self.save into the controller?


Thanks


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.

Ruby on Rails



On Monday, June 29, 2015 at 7:40:58 PM UTC+1, Мурыгин Антон wrote:
My rails version is 4.2.2.


Weird - I created a fresh rails app, added a business model like yours and it works fine. I'd recommended doing the same and seeing whether you can isolate what it is about your app that is different (for example is Business::GeneratedAssociationMethods in the ancestor chain for business? What does Profile::GeneratedAssociationMethods.instance_methods look like?)

Fred


 
понедельник, 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 category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?
 
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.

Ruby on Rails



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=0

See 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!


Rails.application exists only in rails 3 & above and from the log you appear to be running rails 2.3.15. At a guess you've a gem installed that requires rails 3 (or possibly rails 4). If you had run `bundle update` and some of your version constraints were quite loose then that might explain it.

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/e0841338-4683-4685-817e-5603a7a82251%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

This is been driving me nuts for the last week.

The requirement: a Friend has a complete ruby rails app currently (was) running on heroku. It's dead now as they don't support the bamboo stack anymore.

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.

But it just won';t run. Compiles, etc. But whatever I say or do it won't run. I done it all:
* Pointed to a local DB. Postgres - set-up and working on local.
* Moved DB from heroku to local with all data. Fine.
* Compiled it - bundle install.
* Yet when i run it... with ==>  foreman start
13:54:36 web.1     | started with pid 12212
13:54:37 web.1     | I, [2015-06-30T13:54:37.799228 #12212]  INFO -- : listening on addr=0.0.0.0:5000 fd=10
13:54:37 web.1     | I, [2015-06-30T13:54:37.799328 #12212]  INFO -- : worker=0 spawning...
13:54:37 web.1     | I, [2015-06-30T13:54:37.800127 #12212]  INFO -- : master process ready
13:54:37 web.1     | I, [2015-06-30T13:54:37.800917 #12213]  INFO -- : worker=0 spawned pid=12213
13:54:37 web.1     | I, [2015-06-30T13:54:37.801238 #12213]  INFO -- : Refreshing Gem list
13:54:37 web.1     | DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /home/online/kacstbook-engage/vendor/bundle/ruby/1.8/gems/activerecord-2.3.15/lib/activerecord.rb:2)
...... AND THEN .......

tialized constant DEFAULT_HOST
13:56:09 web.1     | #<NoMethodError: undefined method `application' for Rails:Module>
13:56:09 web.1     | E, [2015-06-30T13:56:09.106145 #12225] ERROR -- : reaped #<Process::Status: pid=12235,exited(1)> worker=0
13:56:09 web.1     | I, [2015-06-30T13:56:09.106294 #12225]  INFO -- : worker=0 spawning...
13:56:09 web.1     | I, [2015-06-30T13:56:09.107351 #12239]  INFO -- : worker=0 spawned pid=12239
13:56:09 web.1     | I, [2015-06-30T13:56:09.107525 #12239]  INFO -- : Refreshing Gem list
13:56:09 web.1     | DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /home/online/kacstbook-engage/vendor/bundle/ruby/1.8/gems/activerecord-2.3.15/lib/activerecord.rb:2)
13:56:12 web.1     | /home/online/kacstbook-engage/config/initializers/paperclip.rb:6: warning: already initialized constant DEFAULT_HOST
13:56:12 web.1     | #<NoMethodError: undefined method `application' for Rails:Module>
13:56:12 web.1     | E, [2015-06-30T13:56:12.745513 #12225] ERROR -- : reaped #<Process::Status: pid=12239,exited(1)> worker=0
13:56:12 web.1     | I, [2015-06-30T13:56:12.745670 #12225]  INFO -- : worker=0 spawning...
13:56:12 web.1     | I, [2015-06-30T13:56:12.746711 #12243]  INFO -- : worker=0 spawned pid=12243
13:56:12 web.1     | I, [2015-06-30T13:56:12.746847 #12243]  INFO -- : Refreshing Gem list
13:56:12 web.1     | DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /home/online/kacstbook-engage/vendor/bundle/ruby/1.8/gems/activerecord-2.3.15/lib/activerecord.rb:2)
13:56:15 web.1     | /home/online/kacstbook-engage/config/initializers/paperclip.rb:6: warning: already initialized constant DEFAULT_HOST
13:56:16 web.1     | #<NoMethodError: undefined method `application' for Rails:Module>
13:56:16 web.1     | E, [2015-06-30T13:56:16.003923 #12225] ERROR -- : reaped #<Process::Status: pid=12243,exited(1)> worker=0
13:56:16 web.1     | I, [2015-06-30T13:56:16.004079 #12225]  INFO -- : worker=0 spawning...
13:56:16 web.1     | I, [2015-06-30T13:56:16.005235 #12247]  INFO -- : worker=0 spawned pid=12247
13:56:16 web.1     | I, [2015-06-30T13:56:16.005557 #12247]  INFO -- : Refreshing Gem list
13:56:16 web.1     | DEPRECATION WARNING: require "activerecord" is deprecated and will be removed in Rails 3. Use require "active_record" instead. (called from /home/online/kacstbook-engage/vendor/bundle/ruby/1.8/gems/activerecord-2.3.15/lib/activerecord.rb:2)
13:56:19 web.1     | /home/online/kacstbook-engage/config/initializers/paperclip.rb:6: warning: already initialized constant DEFAULT_HOST
13:56:19 web.1     | #<NoMethodError: undefined method `application' for Rails:Module>
13:56:19 web.1     | E, [2015-06-30T13:56:19.368604 #12225] ERROR -- : reaped #<Process::Status: pid=12247,exited(1)> worker=0

See 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!

Thanx.

Goan.


--
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.

Ruby on Rails

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.

Ruby on Rails Monday, June 29, 2015

All config files described here are the same as my Mac OS's and all works fine in Mac OS.
 
I got the same error in CentOS 6 x86_64:
 
    Ident authentication failed for user 'abelard'
When running the following two commands:

    1. rake db:create
    2. psql -d testforabelard2 -U abelard -h localhost
I got the same error after trying these answers [1](http://stackoverflow.com/a/8102547/1054800) and [2](http://stackoverflow.com/a/2942678/1054800).

My `/var/lib/pgsql/9.4/pg_hba.con`'s content is as follows:

    local   all             all                                     trust
    host    all             all             127.0.0.1/32            trust
    host    all             all             ::1/128                 trust
And there is a blank file `/var/lib/pgsql/9.4/pg_ident.con`

My `database.yml`'s content is as follows:

    development:
      adapter: postgresql
      encoding: unicode
      database: social_stream_development
      pool: 5
      username: abelard
      password: password
And for offering the same params as my Mac OS's, I altered `PostgreSQL`'s user `abelard` :

    testforabelard2=# \du
                List of roles
     Role name | Attributes  | Member of 
    -----------+-------------+-----------
     abelard   | Superuser   | {}
               : Create role   
               : Create DB     
     postgres  | Superuser   | {}
               : Create role   
               : Create DB   
And I can run the command without `-h localhost` successfully:
    
    psql -d testforabelard2 -U abelard
I don't know what things I miss, any advice will be welcome!

--
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.

Ruby on Rails

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.

Ruby on Rails

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

It is working beautifully so I'm happy about that. 
However, can anyone please review the code and let me know if it is correct in terms of OOP?
And if it is best practice for Rails.


Rails is awesome.

Cheers

--
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.

Ruby on Rails

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.

Ruby on Rails

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.

Ruby on Rails

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.

Please let me know if there is more code I can move to the model.

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/95a528c4-37ae-4a23-83b3-329aab76d471%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

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 category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?
 
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.

Ruby on Rails

Hi, i'm pretty new to the Rail and Ruby and today I have a problem that I have to solve (tl;dr: the expert ruby/rails has left our company and now the newbies like me have to work on the projects that he left). So, let's go straight:

I have an app running "normally", login function OK, but then when the user try to reach other sections on the site like this one in the error bellow:

Processing by NewsController#show as HTML
  Parameters: {"id"=>"news-alpha-2-3-8"}
  Rendered application/_pagetitle.html.haml (0.1ms)
  Rendered application/_pagetitle.html.haml (0.0ms)
  Rendered news/show.html.haml within layouts/application (2.5ms)
Completed 500 Internal Server Error in 19ms

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'

And the "undefined method `name' for nil:NilClass" error appears in other sections as well. If nay of you could explain me why this erros shows up, because i've double checked the database and these fields are ok, do I have to check for more specific errors? 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/75143d8d-3c59-4fda-9cab-05e84b90fed4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails



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 category
    super || BusinessCategory.new(name: 'other')
  end
end

Why it keeps throwing 
super: no superclass method `category' for #<Business:0x000001023014b8>
? Am i missing something?
 
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/CABp077WDW1ASHDV9rszhna6BcBpke-vEfxG%2BRqx-%3DFH6uUzOCQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

Hi All,

Thank for the feedback.

Yeah, my next step is to consolidate the two actions together.

Good point on using POST instead of GET.
I will change the link_to helper.

As to using generators, I followed the tutorial and used generators for creating the model and controllers for Articles and Comments.

Cheers






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 do
    resources :comments
    member do 
      get 'rankup'
      get 'rankdown'
    end
  end

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.save
  redirect_to articles_path
end

def 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.save
  redirect_to articles_path
end

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_rank
    article.save

    @start_rank = @start_rank + 1
    end

  @article.destroy
  redirect_to articles_path
end

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.

Ruby on Rails

Hey, but i did not rename it. My model hasn't methods called busyness_category! I though AR generates them according to first argument to 'belongs_to' method, rather than from foreign key name

понедельник, 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 category
    super || BusinessCategory.new(name: 'other')
  end
end

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.

Ruby on Rails

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.

Ruby on Rails

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 category
    super || BusinessCategory.new(name: 'other')
  end
end

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/CAHxKQijrynpvtDbig_5GNwAHq1W0mQqBkp5nRFZNyBTMON_bNw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails

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>

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.